On Friday, 19 October 2012 at 09:36:14 UTC, Jonathan M Davis
wrote:
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
You can use the array.ptr "trick" (or exploit):
void main()
{
immutable int a = 5;
immutable(int)* p1 = [a].ptr;
immutable(int*) p2 = [a].ptr;
}
That said, it is ugly as sin, and "new int(5)" should definitely
supported.
Same thing with structs actually, which can be "agglomerate
constructed", or "default copy constructed" : If you can write
"auto a = T(x);" you should be able to write "auto p = new T(x)";