@@ -470,53 +470,53 @@ $(H2 $(LEGACY_LNAME2 StructLiteral, struct-literal, Struct Literals))
470
470
---
471
471
struct S { int x; float y; }
472
472
473
- int foo(S s) { return s.x; }
474
-
475
- foo( S(1, 2) ); // set field x to 1, field y to 2
476
- foo( S(y: 2, x: 1) ); // same as above
473
+ S s1 = S(1, 2); // set field x to 1, field y to 2
474
+ S s2 = S(y: 2, x: 1); // same as above
475
+ assert(s1 == s2);
477
476
---
478
477
)
479
478
480
- $(P Struct literals are syntactically like function calls.
481
- If a struct has a $(RELATIVE_LINK2 struct-constructor, Constructor )
479
+ $(P
480
+ If a struct has a $(RELATIVE_LINK2 struct-constructor, constructor )
482
481
or a member function named `opCall`, then
483
482
struct literals for that struct are not possible. See also
484
483
$(DDSUBLINK spec/operatoroverloading, FunctionCall, opCall operator overloading)
485
484
for the issue workaround.)
486
- $(P
487
- If the first argument has no name, it will be assigned to the struct field that is defined first lexically.
488
- )
489
- $(P
490
- A named argument is assigned to the struct field with the same name.
491
- It is an error if no such field exists.
492
- )
493
- $(P
494
- Any other argument is assigned to the next lexically defined struct field relative to the preceding argument's struct field.
495
- It is an error if no such field exists, i.e. when the preceding argument assigns to the last struct field.
496
- )
497
- $(P
498
- It is also an error to assign a field more than once.
499
- )
500
- $(P
501
- Any fields not assigned a value are initialized with their respective default initializers.
502
- )
503
- $(NOTE
504
- These rules are consistent with function calls, see $(DDSUBLINK spec/expression, argument-parameter-matching, Matching Arguments to Parameters).
485
+
486
+ $(P Struct literals are syntactically like function calls.)
487
+
488
+ $(PANEL
489
+ Arguments are assigned to fields as follows:
490
+
491
+ 1. If the first argument has no name, it will be assigned to the struct field that is defined first lexically.
492
+ 1. A named argument is assigned to the struct field with the same name.
493
+ It is an error if no such field exists.
494
+ 1. Any other argument is assigned to the next lexically defined struct field relative to the preceding argument's struct field.
495
+ It is an error if no such field exists, i.e. when the preceding argument assigns to the last struct field.
496
+ 1. It is also an error to assign a field more than once.
497
+ 1. Any fields not assigned a value are initialized with their respective default initializers.
498
+
499
+ **Note:**
500
+ These rules are consistent with function calls, see $(DDSUBLINK spec/function, argument-parameter-matching, Matching Arguments to Parameters).
505
501
)
506
502
$(P
507
503
If there is a union field in the struct, only one
508
504
member of the union can be initialized inside a
509
505
struct literal. This matches the behaviour for union literals.
510
506
)
511
507
512
- $(SPEC_RUNNABLE_EXAMPLE_FAIL
508
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
513
509
---
514
- struct S { int x = 1; int y = 2, int z = 3; }
510
+ struct S { int x = 1, y = 2, z = 3; }
515
511
516
512
S s0 = S(y: 5, 6, x: 4); // `6` is assigned to field `z`, which comes after `y`
513
+ assert(s0.z == 6);
514
+
517
515
S s1 = S(y: 5, z: 6); // Field x is not assigned, set to default initializer `1`
518
- S s2 = S(y: 5, x: 4, 5); // Error: field `y` is assigned twice
519
- S s3 = S(z: 2, 3); // Error: no field beyond `z`
516
+ assert(s1.x == 1);
517
+
518
+ //S s2 = S(y: 5, x: 4, 5); // Error: field `y` is assigned twice
519
+ //S s3 = S(z: 2, 3); // Error: no field beyond `z`
520
520
---
521
521
)
522
522
0 commit comments