Named parameters for methods

This forum is meant for questions and discussions about the X# language and tools
Post Reply
fxm
Posts: 53
Joined: Tue Nov 24, 2015 9:27 am
Location: Austria

Named parameters for methods

Post 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
User avatar
Meinhard
Posts: 81
Joined: Thu Oct 01, 2015 4:51 pm

Named parameters for methods

Post 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
fxm
Posts: 53
Joined: Tue Nov 24, 2015 9:27 am
Location: Austria

Named parameters for methods

Post 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
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Named parameters for methods

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
fxm
Posts: 53
Joined: Tue Nov 24, 2015 9:27 am
Location: Austria

Named parameters for methods

Post 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..
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Named parameters for methods

Post by FFF »

Verbose,possibly. But the operators all are relatively hard to ready, with tired eyes...
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
IKavanagh

Named parameters for methods

Post 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.
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Named parameters for methods

Post by FFF »

The colon is the method calling operator in X:
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
Post Reply