The cast(signed), cast(unsigned) feature proposal

Idan Arye GenericNPC at gmail.com
Fri Jun 7 15:05:45 PDT 2013


On Friday, 7 June 2013 at 21:42:05 UTC, Jesse Phillips wrote:
> On Friday, 7 June 2013 at 21:34:00 UTC, Mrzlga wrote:
>> A reason for cast(signed) is to discourage the user from 
>> writing:
>>
>>    cast(int) x;
>>
>> Because we can't grep for the word "signed",
>> We have to search for "cast( )" only!
>>
>>    cast(int) x    // hides the intention, hard to search for!
>>
>> Yet we can easily grep for:
>>
>>    cast(signed)
>
> Not convenient, but:
>
>     cast(Signed!(typeof(x))

How about this: if `Foo` is a template with a single type 
parameter that returns a type, writing `cast(Foo)bar` will be 
parsed as `cast(Foo!(typeof(bar))bar`.

This will allow as to write `cast(Signed)x` - no need for extra 
keywords - and it will also allow us to write more generic 
things, like `cast(Nullable)(x)` will return a `typeof(x)` 
`Nullable` that holds the value of `x` - whatever that value may 
be.

I've written a simple function that does that:

     auto Cast(alias Template,T)(T source){
         return cast(Template!T)(source);
     }

And it works, but not as good as the template solution since 
`typeof(Cast!Nullable(0))` returns `Template!(int)` instead of 
`Nullable!(int)`.


More information about the Digitalmars-d mailing list