digitalmars.D - Associative arrays can't have a static array as key
- Michiel <nomail hotmail.com> Feb 08 2007
- "Jarrett Billingsley" <kb3ctd2 yahoo.com> Feb 08 2007
- Tomas Lindquist Olsen <tomas famolsen.dk> Feb 08 2007
- Michiel <nomail hotmail.com> Feb 08 2007
- "Jarrett Billingsley" <kb3ctd2 yahoo.com> Feb 08 2007
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...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.
Also: foo("blah".dup); will work.
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...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.
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"); } :)
Feb 08 2007









Tomas Lindquist Olsen <tomas famolsen.dk> 