Skip to content

Commit da60a24

Browse files
ntreldlang-bot
authored andcommitted
[spec] Tweak named arguments docs
Follow up to #3744.
1 parent c460dee commit da60a24

File tree

3 files changed

+46
-52
lines changed

3 files changed

+46
-52
lines changed

spec/expression.dd

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,30 +1633,24 @@ f(5, 6);
16331633
$(H4 $(LNAME2 argument-parameter-matching, Matching Arguments to Parameters))
16341634

16351635
$(P
1636-
Arguments in the `NamedArgumentList` are matched to function parameters as follows:
1636+
Arguments in a `NamedArgumentList` are matched to function parameters as follows:
16371637
)
16381638

1639-
$(P
1640-
If the first argument has no name, it will be assigned to the first function parameter.
1641-
)
1642-
$(P
1643-
A named argument is assigned to the function parameter with the same name.
1639+
1. If the first argument has no name, it will be assigned to the first function parameter.
1640+
1. A named argument is assigned to a function parameter with the same name.
16441641
It is an error if no such parameter exists.
1645-
)
1646-
$(P
1647-
Any other argument is assigned to the next parameter relative to the preceding argument's parameter.
1642+
1. Any unnamed argument is assigned to the next parameter relative to the preceding argument's parameter.
16481643
It is an error if no such parameter exists, i.e. when the preceding argument assigns to the last parameter.
1649-
)
1650-
$(P
1651-
Assigning a parameter more than once is an error.
1652-
Not assigning a parameter any argument is also an error,
1644+
1. Assigning a parameter more than once is an error.
1645+
1. Not assigning a parameter an argument is also an error,
16531646
unless the parameter has a $(DDSUBLINK spec/function, function-default-args, Default Argument).
1654-
)
16551647

16561648
$(H4 $(LNAME2 type-constructor-arguments, Constructing a Type with an Argument List))
1657-
$(P A type can precede a list of arguments.
1658-
See: $(DDSUBLINK spec/struct, struct-literal, Struct Literals) and $(RELATIVE_LINK2 uniform_construction_syntax, Uniform construction syntax for built-in scalar types)
1659-
)
1649+
1650+
$(P A type can precede a list of arguments. See:)
1651+
1652+
* $(DDSUBLINK spec/struct, struct-literal, Struct Literals)
1653+
* $(RELATIVE_LINK2 uniform_construction_syntax, Uniform construction syntax for built-in scalar types)
16601654

16611655
$(H3 $(LEGACY_LNAME2 index_operations, index_expressions, Index Operations))
16621656

spec/function.dd

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,16 +1280,16 @@ $(H2 $(LNAME2 function-overloading, Function Overloading))
12801280
)
12811281

12821282
$(OL
1283-
$(LI no match)
1284-
$(LI match with implicit conversions)
1285-
$(LI match with qualifier conversion (if the argument type is
1283+
$(LI No match)
1284+
$(LI Match with implicit conversions)
1285+
$(LI Match with qualifier conversion (if the argument type is
12861286
$(GLOSSARY qualifier-convertible) to the parameter type))
1287-
$(LI exact match)
1287+
$(LI Exact match)
12881288
)
12891289

1290-
$(P Named Arguments are resolved for a candidate according to
1290+
$(P Named arguments are resolved for a candidate according to
12911291
$(DDSUBLINK spec/expression, argument-parameter-matching, Matching Arguments to Parameters).
1292-
If this fails (for example, because the overload does not have a parameter with an argument's assigned named),
1292+
If this fails (for example, because the overload does not have a parameter matching a named argument),
12931293
the level is $(I no match). Other than that, named arguments do not affect the matching level.
12941294
)
12951295

spec/struct.dd

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -470,53 +470,53 @@ $(H2 $(LEGACY_LNAME2 StructLiteral, struct-literal, Struct Literals))
470470
---
471471
struct S { int x; float y; }
472472

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);
477476
---
478477
)
479478

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)
482481
or a member function named `opCall`, then
483482
struct literals for that struct are not possible. See also
484483
$(DDSUBLINK spec/operatoroverloading, FunctionCall, opCall operator overloading)
485484
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).
505501
)
506502
$(P
507503
If there is a union field in the struct, only one
508504
member of the union can be initialized inside a
509505
struct literal. This matches the behaviour for union literals.
510506
)
511507

512-
$(SPEC_RUNNABLE_EXAMPLE_FAIL
508+
$(SPEC_RUNNABLE_EXAMPLE_RUN
513509
---
514-
struct S { int x = 1; int y = 2, int z = 3; }
510+
struct S { int x = 1, y = 2, z = 3; }
515511

516512
S s0 = S(y: 5, 6, x: 4); // `6` is assigned to field `z`, which comes after `y`
513+
assert(s0.z == 6);
514+
517515
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`
520520
---
521521
)
522522

0 commit comments

Comments
 (0)