digitalmars.D - Associative arrays can't have a static array as key
- Michiel (14/14) Feb 08 2007 I have a template function like this:
- Jarrett Billingsley (5/8) Feb 08 2007 foo("blah"[]);
- Tomas Lindquist Olsen (4/15) Feb 08 2007 Also:
- Michiel (2/5) Feb 08 2007 Hm.. Yes. That works. But I was hoping for a solution inside the functio...
- Jarrett Billingsley (16/22) Feb 08 2007 import std.traits;
I have a template function like this: void foo(T)(T bar) { int[T] bla; /* more code */ } And I occasionally want to call it with a string literal. foo("test"); But the compiler sais I can't, because "test" is of type char[4], and apparently associative arrays can't take a static array as key. This isn't documented. In fact, the documentation says that type[dim] can be implicitly converted to type[]. But putting that aside for the moment, how can I convince D to automatically make it a dynamic array, so I can use my function? Thanks!
Feb 08 2007
"Michiel" <nomail hotmail.com> wrote in message news:eqfctr$69r$1 digitaldaemon.com...But putting that aside for the moment, how can I convince D to automatically make it a dynamic array, so I can use my function?foo("blah"[]); Notice the slice operator. This converts the static array into a dynamic one. Kind of ugly, but.
Feb 08 2007
Jarrett Billingsley wrote:"Michiel" <nomail hotmail.com> wrote in message news:eqfctr$69r$1 digitaldaemon.com...Also: foo("blah".dup); will work.But putting that aside for the moment, how can I convince D to automatically make it a dynamic array, so I can use my function?foo("blah"[]); Notice the slice operator. This converts the static array into a dynamic one. Kind of ugly, but.
Feb 08 2007
foo("blah"[]); Notice the slice operator. This converts the static array into a dynamic one. Kind of ugly, but.Hm.. Yes. That works. But I was hoping for a solution inside the function definition, so i could still use regular string literals.
Feb 08 2007
"Michiel" <nomail hotmail.com> wrote in message news:eqfeid$8mt$1 digitaldaemon.com...import std.traits; void foo(T)(T bar) { static if(isStaticArray!(T)) int[typeof(T[0])[]] bla; else int[T] bla; // ... } void main() { foo("hi"); } :)foo("blah"[]); Notice the slice operator. This converts the static array into a dynamic one. Kind of ugly, but.Hm.. Yes. That works. But I was hoping for a solution inside the function definition, so i could still use regular string literals.
Feb 08 2007