Why no implicit cast operators?
Artur Skawina
art.08.09 at gmail.com
Mon Aug 6 08:08:37 PDT 2012
On 08/06/12 15:29, Tommi wrote:
> In D it's not possible to make opCast operators implicit.
> Therefore I see no way of making "transparent wrappers"; like
> structs which could be used as a drop-in replacement for plain
> old data types.
>
> E.g if I wanted to make a SafeInt struct, that behaves otherwise
> just like an int, but when operators like +=, *=, ++, -- etc are
> used with it, my custom SafeInt operators would check that
> there's no integer overflow. If you use alias this to _reveal_
> the internal int value of SafeInt, then that int value's default
> operators are used, and thus no overflow checking.
There's sort-of limited support for implicit-casts - 'alias this'
with a getter. That does not force exposure of the internal
representation, but has other problems. The main one is of course
that there can only be one 'alias this'.
> I find the lack of implicit casting a defect of the language.
Yes.
> I hope that I'm wrong.
You are not.
artur
More information about the Digitalmars-d
mailing list