[Issue 11206] static array can be implicitly built from items, when nested in aggregate

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Oct 9 23:31:13 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=11206



--- Comment #9 from Maxim Fomin <maxim at maxim-fomin.ru> 2013-10-09 23:31:12 PDT ---
(In reply to comment #7)
> The inconsistency comes from the incomplete fix of bug 7019.
> 
> struct Agg(T) { T val; }
> void main()
> {
>     int[3] sa = 1;                // 1a, OK
>     auto agg1 = Agg!(int[3])(1);  // 1b, OK
> 
>     struct S { this(int) {} }
>     S s = 1;                      // 2a, OK, by fixing issue 7019
>     auto agg2 = Agg!S(1);         // 2b
> }
> 
> Since long time ago, 1a and 1b has been allowed.
> 
> On the other hand, from 2.061, 2a has formally become a part of the language
> spec, by fixing issue 7019.
> 
> However, currently 2b is still disallowed.

Formally this is right because struct initializers are not recursive. If
Add!int(1) is a valid struct literal doesn't mean that Agg!S(1) is also valid
because S(1) is valid (by the way, here you placed constructor not literal).
Unfortunately there is no implicit conversion between basic type to struct type
even if the latter is constructable from the former.

> Because, when I had wrote a compiler patch for bug 7019, I had thought that
> accepting it might cause some ambiguity on multi-dimentional array
> initializing.
> 
>   struct S { this(int) {} }
>   S[2][2] sa = [1, 2];
>   // exactly same as:
>   //      sa = [[S(1), S(2)], [S(1), S(2)]]
>   // or:  sa = [[S(1), S(1)], [S(2), S(2)]]
>   // ?
> 
> But recently, I found a new consistent rule to resolve the ambiguity. Based on
> the rule in my brain, the 2b case would also be accepted.
> 
> Therefore, the current inconsistency is a bug to me. If T t = val; is accepted,
> the struct literal syntax Aggr!T(val) should also be accepted.

As I have mentioned previously, there is no inconsistency because you
implicitly assuming that struct literal/constructors can be recursive or
'nested' in some sense. If you mean allowing AGG!S ts1 = AGG!S(1); in original
example, then this is a minor enhancement.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list