xsharp.eu • Named parameters for methods
Page 1 of 1

Named parameters for methods

Posted: Thu Dec 15, 2022 2:43 pm
by fxm
Hey,

Are named parameters possible in X#?

C# Example:

Code: Select all

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

Code: Select all

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

Named parameters for methods

Posted: Thu Dec 15, 2022 3:34 pm
by Meinhard
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

Named parameters for methods

Posted: Thu Dec 15, 2022 7:31 pm
by fxm
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

Named parameters for methods

Posted: Fri Dec 16, 2022 7:10 am
by robert
Fergus,
fxm post=24789 userid=398 wrote: 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

Code: Select all

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

Named parameters for methods

Posted: Fri Dec 16, 2022 1:38 pm
by fxm
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?

Code: Select all

Foo([NamedParam]Param1 := "somevalue")

Although might make the code quite verbose..

Named parameters for methods

Posted: Fri Dec 16, 2022 1:42 pm
by FFF
Verbose,possibly. But the operators all are relatively hard to ready, with tired eyes...

Named parameters for methods

Posted: Sat Dec 17, 2022 10:43 pm
by IKavanagh
Might be naive of me or just late in the day, but why not use

Code: Select all

:
like C#?

Otherwise, I'd go with

Code: Select all

::
. It's easier to type than changing symbol and using

Code: Select all

=
on it's own would be confusing.

Named parameters for methods

Posted: Sun Dec 18, 2022 9:52 am
by FFF
The colon is the method calling operator in X: