int[] as constructor
H. S. Teoh
hsteoh at quickfur.ath.cx
Tue Dec 4 23:28:42 UTC 2018
On Tue, Dec 04, 2018 at 10:17:04PM +0000, jmh530 via Digitalmars-d-learn wrote:
> I've noticed that I can use int like a constructor, as in:
> int x = int(1);
> but I can't do the same thing with slices
> int[] y = int[]([1, 2]);
>
> Is there something I'm missing here or is this a potential
> enhancement? It can make some types of generic code a little more
> annoying.
For built-in types, you can just write:
int[] y = cast(int[]) [ 1, 2 ];
Well OK, for int[] it's kinda silly 'cos that's the default, but in my
code I've often had to write things like:
auto z = cast(float[]) [ 1.0, 2.0, 3.0 ];
because otherwise it would be inferred as double[].
But yeah, this wouldn't work so well in generic code where you might be
constructing a user-defined type. What Andrei said about people not
wanting built-in types to behave differently from user-defined types
holds true here. In an ideal language, there would be no lexical
distinction between the two, and generic code would Just Work(tm). D is
a lot closer to this ideal than many other languages, I daresay even the
majority of languages, but alas, it's not completely there.
T
--
Without geometry, life would be pointless. -- VS
More information about the Digitalmars-d-learn
mailing list