Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

Named parameters for methods 15 Dec 2022 15:43 #24784

  • fxm
  • fxm's Avatar
  • Topic Author


  • Posts: 52
  • Hey,

    Are named parameters possible in X#?

    C# Example:
    public static class Utils
    {
        public static string DoStuff(string Param1 = "one", string Param2 = "two", string Param3 = "three")
        {
            return Param1 + Param2 + Param3;
        }
    }

    Usage:
    Utils.DoStuff(Param1: "one", Param3: "3");

    Thanks,
    Fergus

    Please Log in or Create an account to join the conversation.

    Named parameters for methods 15 Dec 2022 16:34 #24785

    • Meinhard
    • Meinhard's Avatar


  • Posts: 77
  • Hi Fergus,

    on the language tab in the project properties, there is a switch for named arguments. This switch is off for all dialect except core. See documentation for the reason why this is switched off and the consequences it has to switch it on.
    There's some discussion going on, if named and optional parameters are good or bad. From my own experience I know that sometimes they are neccessary if you call some methods implemented in a 3rd party library. But, in my own code I try to prevent them, because there are some potential risks especially in refactoring.I personally prefer to operate with option classes.

    Regards
    Meinhard

    Please Log in or Create an account to join the conversation.

    Named parameters for methods 15 Dec 2022 20:31 #24789

    • fxm
    • fxm's Avatar
    • Topic Author


  • Posts: 52
  • Hey Meinhard,

    Thanks for that - have stared at that screen so many time but I guess I wasn't looking looking for named parameters then.

    Turned it on for the project I was working on and it worked great - it just happened to break a whole bunch of other stuff.

    Have reverted to Plan B.

    Thanks again.

    Fergus

    Please Log in or Create an account to join the conversation.

    Named parameters for methods 16 Dec 2022 08:10 #24792

    • robert
    • robert's Avatar


  • Posts: 3447
  • Fergus,

    Thanks for that - have stared at that screen so many time but I guess I wasn't looking looking for named parameters then.
    Turned it on for the project I was working on and it worked great - it just happened to break a whole bunch of other stuff.

    The problem with the current implementation is that code like
    Foo(Param1 := "somevalue")
    Can both be a named parameter, but it can also be an assignment to Param1 and then send that value to the Foo() function.
    We could prevent that when we choose an alternative operator.

    I was thinking of
    - =
    - ::
    - :==
    - ::=


    What do you think ?

    Robert
    XSharp Development Team
    The Netherlands

    Please Log in or Create an account to join the conversation.

    Named parameters for methods 16 Dec 2022 14:38 #24795

    • fxm
    • fxm's Avatar
    • Topic Author


  • Posts: 52
  • Hi Robert,

    Thanks - that sounds good if it wasn't going to cause trouble.

    I would go with the last option.

    As an alternative, would an attribute be a runner?
    Foo([NamedParam]Param1 := "somevalue")

    Although might make the code quite verbose..

    Please Log in or Create an account to join the conversation.

    Named parameters for methods 16 Dec 2022 14:42 #24796

    • FFF
    • FFF's Avatar


  • Posts: 1398
  • Verbose,possibly. But the operators all are relatively hard to ready, with tired eyes...
    Regards
    Karl (X# 2.14.0.4; Xide 1.33; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Named parameters for methods 17 Dec 2022 23:43 #24800

    • IKavanagh
    • IKavanagh's Avatar


  • Posts: 13
  • Might be naive of me or just late in the day, but why not use
    :
    like C#?

    Otherwise, I'd go with
    ::
    . It's easier to type than changing symbol and using
    =
    on it's own would be confusing.
    Is mise le meas,
    Ian

    Please Log in or Create an account to join the conversation.

    Named parameters for methods 18 Dec 2022 10:52 #24802

    • FFF
    • FFF's Avatar


  • Posts: 1398
  • The colon is the method calling operator in X:
    Regards
    Karl (X# 2.14.0.4; Xide 1.33; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1