October 14, 2014
On Tuesday, 14 October 2014 at 03:34:06 UTC, Ary Borenszweig wrote:
> One simple thing we did in Crystal is to allow invoking a function with named arguments only for arguments that have a default value. For example:
>
> void foo(int x, int y = 2, int z = 3) { ... }
>
> foo(x, y: 10);
> foo(x, y: 10, z: 20);
> foo(x, z: 30)
>
> But not this:
>
> foo(x: 10)
>
> The logic behind this is that named arguments are usually wanted when you want to replace one of the default values while keeping the others' defaults. You could specify names for arguments that don't have a default value, but that only gives a small readability aid. Changing a default value in the middle is a new feature.
>
> This greatly simplifies the logic, since parameter reordering can only happen for names that have default values and you can always fill the gaps. Also, default values can also appear last in a function signature.
Another thought I had is that an alternative could be to have some special syntax to say "use the default for this parameter."
I do not have experience implementing languages so perhaps I am wrong, but it seems like it should be possible for the compiler to get the default value and replace some placeholder with it.

Something like:
void foo(int a = 42, int b = 0){}
foo(@default, 7); //rewritten to foo(42, 7);
October 15, 2014
On Monday, 13 October 2014 at 08:29:42 UTC, 岩倉 澪 wrote:
> From what I've found, there was some work on this in the past (http://forum.dlang.org/thread/wokfqqbexazcguffwiif@forum.dlang.org?page=6#post-thclpgdlfxxhhfklwsoj:40forum.dlang.org), but a pull request was never made/I don't seem to find discussion about adding it as a feature anywhere.
>
> I think optional named parameters would be a nice addition, either to the core language, or something like the monadic solution from that old thread in phobos.
>
> Are there good reasons not to add something like this to the language, or is it simply a matter of doing the work? Has it been discussed much?

That's just taking laziness one step further :)

October 15, 2014
On Tuesday, 14 October 2014 at 21:21:23 UTC, 岩倉 澪 wrote:
> Something like:
> void foo(int a = 42, int b = 0){}
> foo(@default, 7); //rewritten to foo(42, 7);

It is useful to have "_" mean "I don't care" when you have tuples. So you would then write:

"foo( _ , 7 )" and " _,y = get_point()"


October 15, 2014
On Wed, 15 Oct 2014 11:21:43 +0000
via Digitalmars-d <digitalmars-d@puremagic.com> wrote:

> On Tuesday, 14 October 2014 at 21:21:23 UTC, 岩倉 澪 wrote:
> > Something like:
> > void foo(int a = 42, int b = 0){}
> > foo(@default, 7); //rewritten to foo(42, 7);
> 
> It is useful to have "_" mean "I don't care" when you have tuples. So you would then write:
> 
> "foo( _ , 7 )" and " _,y = get_point()"

it better be "__" (two underscores). the rationale is simple: "__" is clearly reserved for internal use, so it can has any meaning we need without breaking any code. 'cause single underscore now can be used as placeholder in `foreach (_; 0..42)`, *AND* still can be accessed as normal var. besides, nested foreach with '_' is not working. but "__" can generate unique temporary variable each time.


October 15, 2014
ketmar:

> besides, nested foreach with '_' is not working. but "__"
> can generate unique temporary variable each time.

The point is to introduce a little breaking change in D and use "_" as "don't care", so you can reuse it for nested scoped and for tuple unpacking, and for other similar future purposes.

Bye,
bearophile
October 15, 2014
On Wednesday, 15 October 2014 at 11:52:13 UTC, bearophile wrote:
> ketmar:
>
>> besides, nested foreach with '_' is not working. but "__"
>> can generate unique temporary variable each time.
>
> The point is to introduce a little breaking change in D and use "_" as "don't care", so you can reuse it for nested scoped and for tuple unpacking, and for other similar future purposes.
>
> Bye,
> bearophile

I agree that _ would be the ideal syntax, but why make a breaking change when it is unnecessary? __ would serve just as well and it is only one extra character. It is already reserved so it would be reasonable to put it to good use.
October 17, 2014
They are discussing about named arguments for C++:

http://www.reddit.com/r/cpp/comments/2jiai2/n4172_named_arguments/
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4172.htm

Bye,
bearophile
October 20, 2014
Slightly off-topic, but I'm learning the constructed (human) language lojban, and the FA and zo'e constructs remind me of named parameters and _.
In lojban there are constructs called selbri that are kind of like a programming languages functions: vecnu = x1 sells x2 to x3 for price x4
x1, x2, x3, x4 representing positional parameters
If you don't want to specify one you pass zo'e which is like the word "something."
If you leave off some sumti (args), it is equivilant to passing zo'e. Five selbri parameters have names, fa, fe, fi, fo, fu. So you can pass them in any order if you put those tags in front.

More on-topic: I don't really think named parameters are a worthwhile feature, but I really hope D gets __. That seems like it wouldn't have any major conflicts with implementing, and would give functions a bit of a boost in terms of expressiveness.
1 2 3
Next ›   Last »