Thread overview |
---|
April 23, 2007 Creating variable name with variables | ||||
---|---|---|---|---|
| ||||
Hello, I was wondering if it is possible to create a variable name with other variables. For example: char[] left = "num1"; char[] right = "num2"; char[] num1num2 = "doesn't matter what this equals"; How would I create the variable with the values of the first two variables? Thanks! |
April 23, 2007 Re: Creating variable name with variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to okibi | okibi wrote:
> Hello,
>
> I was wondering if it is possible to create a variable name with other variables. For example:
>
> char[] left = "num1";
> char[] right = "num2";
>
> char[] num1num2 = "doesn't matter what this equals";
>
> How would I create the variable with the values of the first two variables?
>
> Thanks!
you can't unless left and right are const. If they are you have several options:
mixin("char[] "~left~right~";")
then use mixin(left~right) for the variable
another option is a total hack
template Value(T, char[] str) { T Value; }
Value!(char[], left~right) = "hello";
writef("%s\n", Value!(char[], left~right));
I haven't actually tried that usage, but you can make it work for some cases. The advantage of it is that you can do it with D v1.0
|
April 23, 2007 Re: Creating variable name with variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS Wrote:
> okibi wrote:
> > Hello,
> >
> > I was wondering if it is possible to create a variable name with other variables. For example:
> >
> > char[] left = "num1";
> > char[] right = "num2";
> >
> > char[] num1num2 = "doesn't matter what this equals";
> >
> > How would I create the variable with the values of the first two variables?
> >
> > Thanks!
>
> you can't unless left and right are const. If they are you have several options:
>
> mixin("char[] "~left~right~";")
>
> then use mixin(left~right) for the variable
>
> another option is a total hack
>
> template Value(T, char[] str) { T Value; }
>
> Value!(char[], left~right) = "hello";
> writef("%s\n", Value!(char[], left~right));
>
> I haven't actually tried that usage, but you can make it work for some cases. The advantage of it is that you can do it with D v1.0
They won't be const so that first method is out. I'll give the second method a go and see if it works.
Thanks!
|
April 23, 2007 Re: Creating variable name with variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to okibi | okibi wrote:
> BCS Wrote:
>
>
>>okibi wrote:
>>
>>>Hello,
>>>
>>>I was wondering if it is possible to create a variable name with other variables. For example:
>>>
>>>char[] left = "num1";
>>>char[] right = "num2";
>>>
>>>char[] num1num2 = "doesn't matter what this equals";
>>>
>>>How would I create the variable with the values of the first two variables?
>>>
>>>Thanks!
>>
>>you can't unless left and right are const. If they are you have several options:
>>
>>mixin("char[] "~left~right~";")
>>
>>then use mixin(left~right) for the variable
>>
>>another option is a total hack
>>
>>template Value(T, char[] str) { T Value; }
>>
>>Value!(char[], left~right) = "hello";
>>writef("%s\n", Value!(char[], left~right));
>>
>>I haven't actually tried that usage, but you can make it work for some cases. The advantage of it is that you can do it with D v1.0
>
>
> They won't be const so that first method is out. I'll give the second method a go and see if it works.
>
> Thanks!
if they aren't const than the only option is #3
char[][char[]] vars;
vars[left~right] = something;
but that give you a hash lookup for every access (modulo optimizations).
|
Copyright © 1999-2021 by the D Language Foundation