Is there a way to remove the requirement for parenthesis?

Denis Koroskin 2korden at gmail.com
Wed Jan 28 01:29:59 PST 2009


On Wed, 28 Jan 2009 08:45:12 +0300, Daniel Keep <daniel.keep.lists at gmail.com> wrote:

>
> Honestly, I can't see what you're trying to accomplish.  It looks like
> you want something that's not called int, but which works exactly like
> an int does, and can be passed as one.
>
> If you just want another name for "int", you can use an alias.
>
> From the compiler's POV, there's no difference between "BlockNum" and  
> "int".
>
>> alias int BlockNum;
>> BlockNum a = 42;
>> someFuncThatTakesAnInt(a);
>
> If you want to have a type that is an int, but which won't allow itself
> to directly interact with ints, use a typedef.
>
> Personally, I like this usage for simple numeric values which I don't
> want to accidentally mix with other types.  Yes, it's a bit of a pain to
> do arithmetic, but that's the trade-off you make.
>
> From the compiler's POV, "BlockNum" and "int" are totally distinct,
> incompatible types that just happen to be the same under the hood.
>
>> typedef int BlockNum;
>> BlockNum a = cast(BlockNum) 42;
>> someFuncThatTakesAnInt(cast(int) a);
>
> The last is if you need something that's basically an int, but you want
> it to behave differently.  In that case, a struct with operators is your
> best bet.
>
> Let's say you wanted to do something like a Meters struct to store
> lengths.  I'd do something like this:
>
>> struct Meters {
>>     private int value;
>>     int asInt() { return value; }
>>     int asInt(int v) { return value=v; }
>>     // ... operator overloads ...
>> }
>>
>> Meters a; a.asInt = 42;
>> someFuncThatTakesAnInt( a.asInt );
>
> I can't really offer more than that, since I don't know what it is
> you're trying to accomplish.
>
>   -- Daniel

Perhaps, he wants opImplicitCast?



More information about the Digitalmars-d-learn mailing list