Implicit Conversions in Struct Construction

Jonathan M Davis jmdavisProg at gmx.com
Fri Oct 19 02:35:41 PDT 2012


On Friday, October 19, 2012 10:59:07 bearophile wrote:
> Jonathan M Davis:
> > Except that that won't work for int or other built-in types,
> > because they lack constructors.
> 
> Isn't it possible to modify D and add constructors to built-in
> types?
> 
> int(10)

Of course, it would be possible. Whether Walter would agree to though, I have 
no idea. C++ allows it only because it allows you to use the cast operator in 
reverse - i.e. int(10) is identical to (int)10 - though it wouldn't need it in 
the OP's example, because it would do the implicit conversion.

What I'd like to see even more is the ability to do

auto i = new int(10);

which would be particularly useful with immutable, since right now, it's 
impossible to create an immutable pointer to int with a value of anything 
other than 0 without casting.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list