Spin-off issue from bug 9112.
In current, array types does not have consistency between literal syntax and `new` syntax.
void maiin() {
// dynamic array
int[] da1 = [1,2,3]; // literal syntax
//int[] da2 = int[](1,2,3); // not allowed
// because dynamic array is *always* allocated in heap?
int[] da3 = new int[](3); // heap-allocated int array has length 3.
// but, all elements in da2 initialized with int.init.
// we cannot give initializing elements in use of new syntax.
int[] dax = new int[3]; // special syntax. This creates int[] has length 3.
int[3] sa1 = [1,2,3]; // literal syntax
//int[3] sa2 = int[3](1,2,3); // not allowed
//int[3]* sa3a = new int[3]; // impossible
alias T = int[3];
//int[3]* sa3b = new T;. // Error: new can only create structs, dynamic arrays or class objects, not int[3u]'s
// -> special syntax for dynamic array blocks this...
int[string] aa1 = ["a":1, "b":2]; // literal syntax
//int[string] aa1 = int[string]("a":1, "b":2); // not allowed
//int[string] aa2 = new int[string]; // not allowed
}
This is not serious language flaw, but I feel it is desirable which have consistent syntax.
Comment #1 by bearophile_hugs — 2012-12-06T18:05:57Z
What exactly are the syntax changes/additions are you suggesting?
I think this syntax is better to not support this syntax:
int[] da2 = int[](1,2,3); // not allowed
-------------------
There are several design problems in the array syntax. Like:
Issue 3849
Issue 4703
Issue 7445
One maybe even bigger syntax problem is related to mixing fixed-sized arrays and dynamic arrays. This is not supported (this means a dynamic array of fixed arrays of dynamic arrays):
void main() {
auto a = new int[][3][](5, 7);
}
Currently the D syntax to create such mixes is incomplete, and I think it's not clear enough.
Comment #2 by k.hara.pg — 2012-12-06T18:45:36Z
(In reply to comment #1)
> What exactly are the syntax changes/additions are you suggesting?
I'm not sure what is should be chosen. This is not an exact syntax proposal, rather a suggestion to discuss about the issue.
> I think this syntax is better to not support this syntax:
> int[] da2 = int[](1,2,3); // not allowed
I agree that supporting it (stack-allocated dynamic array) is much difficult.
> There are several design problems in the array syntax. Like:
>
> Issue 3849
> Issue 4703
> Issue 7445
>
>
> One maybe even bigger syntax problem is related to mixing fixed-sized arrays
> and dynamic arrays. This is not supported (this means a dynamic array of fixed
> arrays of dynamic arrays):
>
> void main() {
> auto a = new int[][3][](5, 7);
> }
>
>
> Currently the D syntax to create such mixes is incomplete, and I think it's not
> clear enough.
Thanks for presenting related issues.
Comment #3 by robert.schadek — 2024-12-13T18:03:10Z