digitalmars.D - Associative Array Initializers
- Joseph Bell (7/7) Jan 21 2007 Another question regarding arrays, this time associative arrays.
- Frits van Bommel (2/3) Jan 21 2007 No, that's one of the big missing things.
- Serg Kovrov (4/5) Jan 22 2007 Exactly!
- Bill Baxter (11/16) Jan 22 2007 Seems like with tuples now it might be possible to make a function that
- Joseph Bell (6/27) Jan 22 2007 Certainly, anything is possible, but honestly, its a headache. Is there...
- Bill Baxter (6/11) Jan 22 2007 Yep. There's a bugzilla:
- Chris Nicholson-Sauls (10/31) Jan 22 2007 I actually have an Assoc! template in Cashew for this, except the syntax...
- Kevin Bealer (50/71) Jan 25 2007 I don't know why all this syntax is needed; in particular, without the
- Bill Baxter (3/29) Jan 25 2007 make it 'inout a', maybe?
- Kevin Bealer (4/35) Jan 25 2007 I haven't checked to see whether that works - but even if it does, I
- Bill Baxter (6/43) Jan 25 2007 Well it doesn't work. You can't just say inout a, and since you don't
- Bill Baxter (9/46) Jan 25 2007 How about this, though:
Another question regarding arrays, this time associative arrays. int[char[]] months = ["Jan" : 1, "Feb" : 2, etc. definitely doesn't work as intended, that is supplying a initializing set of keys and values. Does D currently support an initialization syntax for associative arrays? Thanks, Joe
Jan 21 2007
Joseph Bell wrote:Does D currently support an initialization syntax for associative arrays?No, that's one of the big missing things.
Jan 21 2007
Frits van Bommel wrote:No, that's one of the big missing things.Exactly! -- serg.
Jan 22 2007
Serg Kovrov wrote:Frits van Bommel wrote:Seems like with tuples now it might be possible to make a function that would do the trick: int[char[]] months = AA("Jan", 1, "Feb", 2, "Mar", 3); This could be another interesting challenge akin to Andrei's max() challenge. Write a function that constructs an AA from it's arguments using the "best" types. For that matter it would be interesting to see a template which constructs an array using the best type to hold the arguments (as opposed to just the type of the first argument). --bbNo, that's one of the big missing things.Exactly!
Jan 22 2007
Certainly, anything is possible, but honestly, its a headache. Is there a specific forum or tracking mechanism to monitor or provide input on language feature requests? From the previous posts to this thread it seems this one would be a candidate for a subsequent revision. Joe Bill Baxter wrote:Serg Kovrov wrote:Frits van Bommel wrote:Seems like with tuples now it might be possible to make a function that would do the trick: int[char[]] months = AA("Jan", 1, "Feb", 2, "Mar", 3); This could be another interesting challenge akin to Andrei's max() challenge. Write a function that constructs an AA from it's arguments using the "best" types. For that matter it would be interesting to see a template which constructs an array using the best type to hold the arguments (as opposed to just the type of the first argument). --bbNo, that's one of the big missing things.Exactly!
Jan 22 2007
Joseph Bell wrote:Certainly, anything is possible, but honestly, its a headache. Is there a specific forum or tracking mechanism to monitor or provide input on language feature requests? From the previous posts to this thread it seems this one would be a candidate for a subsequent revision.Yep. There's a bugzilla: http://d.puremagic.com/issues/ I'd bet that a request for an assoc array literal syntax is already in there. --bb
Jan 22 2007
Bill Baxter wrote:Serg Kovrov wrote:I actually have an Assoc! template in Cashew for this, except the syntax is: int[char[]] months = Assoc!( ["Jan", "Feb", "Mar"], [1 , 2 , 3 ] ); Your "inline" means might be better, though, now that we have Tuples. Also, for the moment, the "workaround" is to initialize the associative array in a module constructor. I would love to see this fixed ASAP. (Feels like such a sore spot.) -- Chris Nicholson-SaulsFrits van Bommel wrote:Seems like with tuples now it might be possible to make a function that would do the trick: int[char[]] months = AA("Jan", 1, "Feb", 2, "Mar", 3); This could be another interesting challenge akin to Andrei's max() challenge. Write a function that constructs an AA from it's arguments using the "best" types. For that matter it would be interesting to see a template which constructs an array using the best type to hold the arguments (as opposed to just the type of the first argument). --bbNo, that's one of the big missing things.Exactly!
Jan 22 2007
Bill Baxter wrote:Serg Kovrov wrote:I don't know why all this syntax is needed; in particular, without the a.dup, it crashes. I don't know why -- it might be a bug or just a bad assumption on my part. // -*- c++ -*- import std.stdio; import std.string; import std.traits; template AA_types(E...) { static if(isStaticArray!(typeof(E[0]))) { alias typeof(E[0].init)[] AA_key; alias typeof(E[1]) AA_value; alias AA_value[AA_key] AA_type; } else { alias typeof(E[0]) AA_key; alias typeof(E[1]) AA_value; alias AA_value[AA_key] AA_type; } } AA_types!(E).AA_type AA(E...)(E values) { static assert(values.length); static assert((values.length % 2) == 0); alias AA_types!(E).AA_key TKey; alias AA_types!(E).AA_value TValue; alias AA_types!(E).AA_type TArray; TKey K; TArray rv; foreach(i, a; values) { static if ((i & 1) == 0) { static if (is(typeof(a.length))) { K = a.dup; } else { K = a; } } static if ((i & 1) == 1) { rv[K] = a; } } return rv; } int main(char[][] args) { auto f = AA("run", 2, "the", 1, "you long", 4); foreach(i, a; f) { writefln("%s -> %s", i, a); } return 0; }Frits van Bommel wrote:Seems like with tuples now it might be possible to make a function that would do the trick: int[char[]] months = AA("Jan", 1, "Feb", 2, "Mar", 3); This could be another interesting challenge akin to Andrei's max() challenge. Write a function that constructs an AA from it's arguments using the "best" types. For that matter it would be interesting to see a template which constructs an array using the best type to hold the arguments (as opposed to just the type of the first argument). --bbNo, that's one of the big missing things.Exactly!
Jan 25 2007
Kevin Bealer wrote:Bill Baxter wrote:make it 'inout a', maybe? --bbSerg Kovrov wrote:I don't know why all this syntax is needed; in particular, without the a.dup, it crashes. I don't know why -- it might be a bug or just a bad assumption on my part.Frits van Bommel wrote:Seems like with tuples now it might be possible to make a function that would do the trick: int[char[]] months = AA("Jan", 1, "Feb", 2, "Mar", 3); This could be another interesting challenge akin to Andrei's max() challenge. Write a function that constructs an AA from it's arguments using the "best" types. For that matter it would be interesting to see a template which constructs an array using the best type to hold the arguments (as opposed to just the type of the first argument). --bbNo, that's one of the big missing things.Exactly!
Jan 25 2007
Bill Baxter wrote:Kevin Bealer wrote:I haven't checked to see whether that works - but even if it does, I still don't see *why* it's more legal. KevinBill Baxter wrote:make it 'inout a', maybe? --bbSerg Kovrov wrote:I don't know why all this syntax is needed; in particular, without the a.dup, it crashes. I don't know why -- it might be a bug or just a bad assumption on my part.Frits van Bommel wrote:Seems like with tuples now it might be possible to make a function that would do the trick: int[char[]] months = AA("Jan", 1, "Feb", 2, "Mar", 3); This could be another interesting challenge akin to Andrei's max() challenge. Write a function that constructs an AA from it's arguments using the "best" types. For that matter it would be interesting to see a template which constructs an array using the best type to hold the arguments (as opposed to just the type of the first argument). --bbNo, that's one of the big missing things.Exactly!
Jan 25 2007
Kevin Bealer wrote:Bill Baxter wrote:Well it doesn't work. You can't just say inout a, and since you don't know the type you can't use "inout Type a". But looking at it again, I'm not sure why I thought that would work in the first place. --bbKevin Bealer wrote:I haven't checked to see whether that works - but even if it does, I still don't see *why* it's more legal. KevinBill Baxter wrote:make it 'inout a', maybe? --bbSerg Kovrov wrote:I don't know why all this syntax is needed; in particular, without the a.dup, it crashes. I don't know why -- it might be a bug or just a bad assumption on my part.Frits van Bommel wrote:Seems like with tuples now it might be possible to make a function that would do the trick: int[char[]] months = AA("Jan", 1, "Feb", 2, "Mar", 3); This could be another interesting challenge akin to Andrei's max() challenge. Write a function that constructs an AA from it's arguments using the "best" types. For that matter it would be interesting to see a template which constructs an array using the best type to hold the arguments (as opposed to just the type of the first argument). --bbNo, that's one of the big missing things.Exactly!
Jan 25 2007
Kevin Bealer wrote:Bill Baxter wrote:How about this, though: foreach(i, a; values) { static if ((i & 1) == 1) { rv[ values[i-1] ] = a; } } Seems to work. --bbKevin Bealer wrote:I haven't checked to see whether that works - but even if it does, I still don't see *why* it's more legal. KevinBill Baxter wrote:make it 'inout a', maybe? --bbSerg Kovrov wrote:I don't know why all this syntax is needed; in particular, without the a.dup, it crashes. I don't know why -- it might be a bug or just a bad assumption on my part.Frits van Bommel wrote:Seems like with tuples now it might be possible to make a function that would do the trick: int[char[]] months = AA("Jan", 1, "Feb", 2, "Mar", 3); This could be another interesting challenge akin to Andrei's max() challenge. Write a function that constructs an AA from it's arguments using the "best" types. For that matter it would be interesting to see a template which constructs an array using the best type to hold the arguments (as opposed to just the type of the first argument). --bbNo, that's one of the big missing things.Exactly!
Jan 25 2007