int[] as constructor
    Jonathan M Davis 
    newsgroup.d at jmdavisprog.com
       
    Tue Dec  4 22:35:48 UTC 2018
    
    
  
On Tuesday, December 4, 2018 3:17:04 PM MST 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.
Using parens with dynamic arrays already has a different meaning. It's how
you provide the size of the dynamic array. e.g.
auto x = int[](12);
or
auto x = int[][](3, 4);
In the first level, you can put the number in between the brackets instead -
e.g.
auto x = int[12];
but that falls apart at deeper levels, because the number in between the
brackets would then mean a static array (making it so that you have a
dynamic array of a static array). So, in the general case, parens are how
you provide a dynamic array's length. This was true long before it became
possible to use parens for construction with built-in types like you do with
user-defined types.
- Jonathan M Davis
    
    
More information about the Digitalmars-d-learn
mailing list