digitalmars.D - Should Tuple!( T, "name" ) be implicitly castable to Tuple!T?
- Simen kjaeraas (40/40) Dec 21 2010 I've noticed that (std.typecons') tuples now are assignable to
- bearophile (10/37) Dec 23 2010 I think it's OK. But the documentation needs to explain about this case.
I've noticed that (std.typecons') tuples now are assignable to structurally equal tuples. I'm not sure I agree fully on the choices made, and figured a discussion might be in order. 1: Tuple!(int, "a") a; Tuple!int b; b = a; IMO, this is good and correct. A tuple without field names is to me a mere black box with data inside, and throwing some other data in there is ok. 2: Tuple!( int, "a" ) a; Tuple!int b; a = b; This, I am not so sure about. A black box is turned into structured data. I guess it's ok. 3: Tuple!( int, "a" ) a; Tuple!( int, "b" ) b; a = b; This I feel, is wrong. a is of a different type from b. 4: void foo( Tuple!int ); Tuple!(int, "a") a; foo( a ); This should work. It may be that alias this is not yet good enough to make this work, but I believe it should be doable by having tuples with named fields use alias this to turn into tuples without named fields, and tuples without named fields taking care of []-lookup. Basically: struct Tuple(T...) if(containsStrings!T) { Tuple!(noStrings!T) get() {...} alias get this; } struct Tuple(T...) if(!containsStrings!T) { T field; alias field this; } -- Simen
Dec 21 2010
Simen kjaeraas:1: Tuple!(int, "a") a; Tuple!int b; b = a; IMO, this is good and correct. A tuple without field names is to me a mere black box with data inside, and throwing some other data in there is ok.I agree.2: Tuple!( int, "a" ) a; Tuple!int b; a = b; This, I am not so sure about. A black box is turned into structured data. I guess it's ok.I think it's OK. But the documentation needs to explain about this case.3: Tuple!( int, "a" ) a; Tuple!( int, "b" ) b; a = b; This I feel, is wrong. a is of a different type from b.I think tuples are meant to be purely structural types, so even adding field names they are compatibile. It's a design choice. In Python they have recently added collections.namedtuple, they are tuples with an optional name: http://docs.python.org/dev/library/collections.html#collections.namedtuple If Phobos tuple gain an optional name as I have once suggested, then two tuples with different names are two different types (so we are back to nominative typing).4: void foo( Tuple!int ); Tuple!(int, "a") a; foo( a ); This should work.If the case 1) works, then this case too has to work. Bye, bearophile
Dec 23 2010