Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
April 29, 2014 tuple can write [],but can't read [] | ||||
---|---|---|---|---|
| ||||
Hi,erveryone, type Tuple!(int,int,int,string) can write[],but can't read[]; module main; import std.stdio,std.typecons,std.conv; void main(string[] argv) { alias Tuple!(int,int,string) tuple2; alias Tuple!(int,int,string)[10] tupleS2; void bbx(tupleS2 x) { foreach(v;x) { writeln(v); foreach(k;v) writeln(k); } } tupleS2 xy2; foreach(i,v;xy2) { xy2[i] = tuple2(1,-1,"xy2 :"~i.to!string); } xy2[1][0]=100; // can write bbx(xy2); for(int i=0;i<3;i++) { writeln(xy2[0][i]); // can't read } } -------------------code end---------------------- if use the 'for(int i=0;i<3;i++)' ,then error. Error: no [] operator overload for type Tuple!(int, int, int, int, int, int, int, int, int, int, int, string) Thank you. Frank |
April 29, 2014 Re: tuple can write [],but can't read [] | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | On Tuesday, 29 April 2014 at 09:23:03 UTC, FrankLike wrote:
> Hi,erveryone,
> [...]
> xy2[1][0]=100; // can write
> [...]
> writeln(xy2[0][i]); // can't read
> [...]
If I'm right, index should be a compile-time value.
|
April 29, 2014 Re: tuple can write [],but can't read [] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | Andrea Fontana:
> If I'm right, index should be a compile-time value.
Right. Because tuples in general don't contain N values of the same type (as in your case), so the compiler has to know statically the index to compute their position efficiently.
Further similar questions are better asked in D.learn.
I also suggest to not mix tab and spaces to indent code, configure your editor to use only spaces or only tabs (4 spaces is the D standard).
Bye,
bearophile
|
April 29, 2014 Re: tuple can write [],but can't read [] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | On Tuesday, 29 April 2014 at 09:38:45 UTC, Andrea Fontana wrote:
> On Tuesday, 29 April 2014 at 09:23:03 UTC, FrankLike wrote:
>> Hi,erveryone,
>> [...]
>> xy2[1][0]=100; // can write
>> [...]
>> writeln(xy2[0][i]); // can't read
>> [...]
>
> If I'm right, index should be a compile-time value.
index is exists,but not be read,why?
Thank you.
|
April 29, 2014 Re: tuple can write [],but can't read [] | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | On Tuesday, 29 April 2014 at 09:23:03 UTC, FrankLike wrote:
> Hi,erveryone,
>
> type Tuple!(int,int,int,string) can write[],but can't read[];
>
> module main;
> import std.stdio,std.typecons,std.conv;
>
> void main(string[] argv)
> {
> alias Tuple!(int,int,string) tuple2;
> alias Tuple!(int,int,string)[10] tupleS2;
>
> void bbx(tupleS2 x)
> {
> foreach(v;x)
> {
>
>
> writeln(v);
> foreach(k;v)
> writeln(k);
> }
> }
> tupleS2 xy2;
>
> foreach(i,v;xy2)
>
>
> {
> xy2[i] = tuple2(1,-1,"xy2 :"~i.to!string);
> }
> xy2[1][0]=100; // can write
> bbx(xy2);
>
>
>
> for(int i=0;i<3;i++)
> {
>
>
> writeln(xy2[0][i]); // can't read
> }
>
> }
> -------------------code end----------------------
>
> if use the 'for(int i=0;i<3;i++)' ,then error.
>
> Error: no [] operator overload for type Tuple!(int, int, int,
> int, int, int, int, int, int, int, int, string)
>
> Thank you.
>
> Frank
Tuple indexes must be compile-time values. This will work:
import std.stdio, std.typecons, std.conv, std.typetuple;
void main()
{
alias Tuple!(int, int, string) tuple2;
alias Tuple!(int, int, string)[10] tupleS2;
void bbx(tupleS2 x)
{
foreach(v; x)
{
writeln(v);
foreach(k;v)
writeln(k);
}
}
tupleS2 xy2;
foreach(i, v; xy2)
{
xy2[i] = tuple2(1, -1, "xy2 :" ~ i.to!string);
}
xy2[1][0]=100;
bbx(xy2);
foreach(i; TypeTuple!(0,1,2))
{
writeln(xy2[0][i]);
}
}
|
April 30, 2014 Re: tuple can write [],but can't read [] | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | > On Tuesday, 29 April 2014 at 09:53:29 UTC, John Colvin wrote:
> foreach(i; TypeTuple!(0,1,2))
> {
> writeln(xy2[0][i]);
> }
> }
Thank you,John Colvin,
It works very good.
Frank.
|
Copyright © 1999-2021 by the D Language Foundation