| Thread overview | |||||
|---|---|---|---|---|---|
|
June 03, 2015 Assoc array typesafe variadic functions | ||||
|---|---|---|---|---|
| ||||
I'm a little puzzled by the fact that typesafe variadic functions may be declared to take an associative array, but there seems to be no way of calling the function to take advantage of this.
ie. foo is a valid function when declared as:
void foo(int[string] bar...)
{
import std.stdio;
bar.writeln;
}
But the only way to call it seems to be to pass an assoc array as a parameter,
foo(["a" : 1, "b" : 2]); // Valid
foo("a", 1, "b", 2); // Not valid
foo("a" : 1, "b" : 2); // Syntax error
I also can't find any mention of this in the language reference ( http://dlang.org/function.html#variadic ). I assume that the syntax is only valid because it would take an additional specialized language rule to do otherwise, however, it might be neat to actually have this as a feature.
foo("a" : 1, "b" : 2); // This would be really cool.
| ||||
June 03, 2015 Re: Assoc array typesafe variadic functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mint | On Wednesday, 3 June 2015 at 19:09:52 UTC, Mint wrote: > I'm a little puzzled by the fact that typesafe variadic functions may be declared to take an associative array, but there seems to be no way of calling the function to take advantage of this. > > ie. foo is a valid function when declared as: > > void foo(int[string] bar...) > { > import std.stdio; > bar.writeln; > } > > But the only way to call it seems to be to pass an assoc array as a parameter, defining a assoc array parameter would make me expect exactly that: that it takes a assoc array as a paramter. > > foo(["a" : 1, "b" : 2]); // Valid > > foo("a", 1, "b", 2); // Not valid > > foo("a" : 1, "b" : 2); // Syntax error > > I also can't find any mention of this in the language reference ( http://dlang.org/function.html#variadic ). I assume that the syntax is only valid because it would take an additional specialized language rule to do otherwise, however, it might be neat to actually have this as a feature. > > foo("a" : 1, "b" : 2); // This would be really cool. whats the benefit of implementing this special case ? you save exactly 2 keystrokes ? | |||
June 04, 2015 Re: Assoc array typesafe variadic functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to extrawurst | On Wednesday, 3 June 2015 at 22:16:03 UTC, extrawurst wrote: > defining a assoc array parameter would make me expect exactly that: that it takes a assoc array as a paramter. Sure then, but the variadic declaration should hold significance too, no? I would expect a function declared as foo(int[string] value); to take an assoc array parameter, but a function declared as foo(int[string] value...); to also provide some sort of variadic functionality on top of that. The resolution would be to either offer a calling syntax that can take advantage of this, or to make such a declaration to produce an error. As it is, there exists a valid, undocumented parameter declaration type in the language that serves no purpose. > whats the benefit of implementing this special case ? you save exactly 2 keystrokes ? The same can be said of any variadic function, as they could all be called in the same manner, foo([1, 2, 3]); // vs. foo(1, 2, 3); The benefit is that of syntactic neatness and visual balance, as well as helping with readability. | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply