How to define opCast from native types

Cherry cherry at dream.land
Fri Feb 7 19:32:23 PST 2014


> Is this good enough?
>
> alias Bitv48 = BitVector!48;
> ulong ull;
> auto foo = ull.toBitV!Bitv48;
>
> Where toBitV is a little function of yours.
>

Hello Bearophile

As I put in the earlier post, in general the functioning of "to" 
in std.conv seems to be that it will not cast away bits. 
ull.to!int actually throws an exception:

std.conv.ConvOverflowException@/../../src/phobos/std/conv.d(1432): 
Conversion positive overflow

And so I do not want to code a toBitV that casts away bits.

What I am doing at the moment is:

alias BitV48 = BitVector!48;
ulong ull;
BitV48 bits = cast(Bit48) ull.toBitVector;

Here toBitVector converts any long to BitVector!64, and int to 
BitVector!32 and so on. And since an explicit cast is required, 
the user knows that he is *cast*ing away bits.

Still my point is that I would like BitVector!48 to behave as 
much native as it could be. And I miss a provision to cast 
directly from long. If we could have opCastRight (actually 
opCastLeft), such issues could be taken care of.

One of the strengths of DLang is that it is a systems programming 
language. But at places I find that D does lack some capabilities 
to create native-like value types.

Regards
- Cherry


More information about the Digitalmars-d mailing list