Array literals are weird.
Adam D. Ruppe
destructionator at gmail.com
Sat May 1 11:50:27 UTC 2021
On Saturday, 1 May 2021 at 11:32:47 UTC, Blatnik wrote:
> It also means that functions that take a slice parameter:
> Can't be called naturally in @nogc or -betterC.
There's several other options you can consider, including making
foo take a static array, in which case this syntax just works, or
an array variadic:
foo(int[] a...)
when you can call it like this:
foo(1, 2, 3)
or you can use the library `.staticArray` thing to expressly
indicate your intention on the original foo
foo([1,2,3].staticArray);
> This obviously isn't a huge deal but it introduces a bit of
> friction into the @nogc workflow. It also makes slower,
> allocating code easier to write by default in non @nogc code.
It also makes you less likely to accidentally write
use-after-free code that refers to an array on the stack after
the function returns...
There's some new features that are supposed to help catch this
even with static arrays (the -dip1000 switch) but that's not
terribly widespread.
> But I'm still new to D, so maybe there is something that I'm
> missing. Any thoughts?
Well, a lot of this is just history: D didn't have static arrays
like it does now for a while, but a good part of it too is to
make the default thing be less likely to cause crash problems by
accidentally escaping a reference to the stack.
More information about the Digitalmars-d
mailing list