New with alias

Bastiaan Veelo Bastiaan at Veelo.net
Tue Mar 10 08:12:00 UTC 2020


On Tuesday, 10 March 2020 at 06:09:23 UTC, tcak wrote:
> I write a code as below:
>
> auto result = new char[4];
>
> It allocates memory as expected.

This is a slice of four chars, which can be used as a dynamic 
array.

> Later I define an alias and do the above step:
>
> alias Pattern = char[4];

This is an alias for a static array with a fixed length of four 
chars.

> auto result = new Pattern;
>
> But compiler says:
> Error: new can only create structs, dynamic arrays or class 
> objects, not `char[4]`'s
>
> Is this a bug, or `alias` doesn't work how I was thinking?

It is not a bug. You cannot new static arrays. You can do this, 
though:

     Pattern pattern; // One static array of four chars.
     auto patterns = new Pattern[3]; // A slice with three static 
arrays of four chars.


--Bastiaan.



More information about the Digitalmars-d-learn mailing list