Array literals are weird.

H. S. Teoh hsteoh at quickfur.ath.cx
Wed May 5 00:45:52 UTC 2021


On Tue, May 04, 2021 at 09:49:16PM +0000, Blatnik via Digitalmars-d wrote:
[...]
> ```
> // With H.S. Teoh's thing:
> auto a = v(1,2,3) + v(3,2,1); // You can do this - a is int[3].
> auto a = v(1,2,3) + v(3,2,1) + v(42,42,42); // But not this - Error: Invalid
> array operation.
> ```
[...]

The only reason it's invalid is because I made opBinary return E[n]
rather than Vec!(E,n). If it returned Vec instead, then arbitrary
expressions can be made possible (e.g., with generic overloads for
opBinary and opUnary). With alias this, the Vec struct will become a
thin wrapper over static arrays; `auto` will give you Vec!(E,n) rather
than E[n], but any use of it that needs E[n] will automatically decay to
E[n].

It's not 100% the same as built-in static array expressions, but it's
pretty darned close.


T

-- 
He who does not appreciate the beauty of language is not worthy to bemoan its flaws.


More information about the Digitalmars-d mailing list