typedefs are useless
John Demme
me at teqdruid.com
Mon Dec 3 17:56:51 PST 2007
Peter C. Chapin wrote:
> Steven Schveighoffer wrote:
>
>> Let's say I want a way to create a type that's like a long, but is not
>> implicitly convertable from a long.
>>
>> I can do:
>>
>> typedef long mytype;
>>
>> However, I can't create literals of this type. so if I want to
>> initialize a mytype value to 6, I have to do:
>>
>> mytype x = cast(mytype)6L;
>
> FWIW, Ada solves this problem by considering literals in a special type
> called "universal integer." It's special because you can't actually
> declare any variables of that type. However, universal integers can be
> implicitly converted to other types derived from Integer. So, in Ada it
> looks like this
>
> type My_Type is range 0..10 -- Or whatever range you need.
>
> X : My_Type;
> Y : Integer;
>
> ...
>
> X := Y; -- Type mismatch. Compile error.
> X := 1; -- Fine. Universal integer converts to My_Type.
>
> This sounds like what you want for D. Note, by the way, that the range
> constraint on a type definition in Ada must be static. Thus the compiler
> can always tell if the value of the universal integer (which can only be
> a literal) is in the right range.
>
> Ada also has a concept of universal float to deal with float point
> literals in a similar way.
>
> Peter
Behavior like this would come about as a result of polysemous values being
added to D, yes?
--
~John Demme
me at teqdruid.com
More information about the Digitalmars-d
mailing list