Should Tuple!( T, "name" ) be implicitly castable to Tuple!T?

bearophile bearophileHUGS at lycos.com
Thu Dec 23 01:12:03 PST 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


More information about the Digitalmars-d mailing list