Named arguments via struct initialization in functions
Martin Tschierschke via Digitalmars-d
digitalmars-d at puremagic.com
Wed Mar 9 05:39:57 PST 2016
On Wednesday, 9 March 2016 at 12:55:16 UTC, Idan Arye wrote:
[...]
>> An other point on my wish list would be to allow string symbol
>> notation
>> like in ruby. Than using hashes (AA) for parameters gets more
>> convenient:
>>
>> :symbol <= just short for => "symbol"
>>
>> h[:y]= 50; h[:x] = 100; // <=> h["y"] = 50; h["x"] = 100
> String symbols are Ruby's(and many Lisps', and maybe some
> other, less popular languages) way to do untyped enums and
> untyped structs. It's a dynamically typed languages thing and
> has no room in statically typed languages like D. D mimics
> dynamic typing to some extent by creating types on the fly with
> it's powerful templates mechanism - but a new type still needs
> to be created. D is geared toward reflection on types at
> compile time, not towards type detection at run-time...
Thats true.
>
> Allowing something like `auto params = [:y : 50, :x : 100]`
> won't really solve anything. It works nicely in Ruby, because
> Ruby has dynamic typing and with some syntactic sugar you get
> elegant syntax for dynamic structs. But in a structured
> language like D, you run into the problem that `[:y : 50, :x :
> 100] is an associative array with a determined type for it's
> values, so you can't do things like `[:y : 50, :x : "hello"]` -
> which greatly limits the usability of this syntax.
Yes.Ok.
What I like about the :symbol notation is, that a string witch is
used
only to distinguish between different objects in an Hash / AA has
a complete different
purpose than a string used to be displayed for the user.
I think that
writeln("Name", a[:name]); is easier to read, than
writeln("Name", a["name"]);
especially if the structures are getting bigger, or you are in a
vibe.d jade template string where you would have to use
additional quoting to write:
a(href="a[\"url\"]") a["link_text"]
a(href="a[:url]") a[:link_text]
May be I should get rid of this by using a struct for my mysql
results to display?
(=> a.url and a.link_text )
Just my 2 Cents :-)
More information about the Digitalmars-d
mailing list