Why no implicit cast operators?

Tommi tommitissari at hotmail.com
Mon Aug 6 06:29:47 PDT 2012


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.

I find the lack of implicit casting a defect of the language.
I hope that I'm wrong.


More information about the Digitalmars-d mailing list