Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 08, 2007 Associative arrays can't have a static array as key | ||||
---|---|---|---|---|
| ||||
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! |
February 08, 2007 Re: Associative arrays can't have a static array as key | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michiel | "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. |
February 08, 2007 Re: Associative arrays can't have a static array as key | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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.
|
February 08, 2007 Re: Associative arrays can't have a static array as key | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | > 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.
|
February 08, 2007 Re: Associative arrays can't have a static array as key | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michiel | "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"); } :) |
Copyright © 1999-2021 by the D Language Foundation