Slice allocation after appending

frame frame86 at live.com
Wed Dec 23 16:14:30 UTC 2020


On Wednesday, 23 December 2020 at 11:19:38 UTC, Rekel wrote:

> I'm not sure what your aliasSeq does, sadly I don't find the 
> documentation's explanation satisfactory.

Try to write

static foreach (arr; [a, b]) { .. }

- it will not work because that loop is generated at compile time 
(static). It will generate the inside code for every usage later 
in code and for each element (a and b) so the 2 lines inside will 
be compiled to 4 lines really when you call info().

But the compiler cannot access the values of a and b at compile 
time (which it thinks it should by using "static" in this case) 
so we need to say the compiler:

- do not use value or address of [a, b] directly
- instead, use [a, b] as symbol - just prepare the code for later 
usage

This is what AliasSeq! does in this example. You can put all 
static stuff inside an AliasSequence:

41      = literal/value
int     = type
int[]   = type
int[] a = symbol/alias


Here it contains not values but the symbols to it. That 
implementation can become very handy for some situations but for 
this simple case

foreach (arr; [a, b]) { .. }

would also work. The difference is that the foreach loop is 
happen at runtime and will not compiled as multiple lines.




More information about the Digitalmars-d-learn mailing list