Skip to content

Commit 1a68865

Browse files
authored
[spec/function] Improve safe variadic docs (#4313)
Reword first paragraph (partly as class variadics are deprecated). Explain array variadics, make it clearer an array can be passed. Make examples runnable; use `assert`. Explain basic type variadic. Fixes #4311.
1 parent b369fee commit 1a68865

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

spec/function.dd

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,14 +2407,16 @@ Bar
24072407
$(H4 $(LNAME2 typesafe_variadic_functions, Typesafe Variadic Functions))
24082408

24092409
$(P A typesafe variadic function has D linkage and a variadic
2410-
parameter declared as either an array or a class.
2411-
The array or class is constructed from the arguments, and
2412-
is passed as an array or class object.
2410+
parameter, which is typically an array. When passing component
2411+
arguments (e.g. array elements), the variadic parameter is
2412+
implicitly constructed from the given arguments.
24132413
)
24142414

2415-
$(P For dynamic arrays:)
2415+
$(P A dynamic array variadic parameter accepts either a
2416+
dynamic array argument or any number of arguments, each of
2417+
which must implicitly convert to the array element type:)
24162418

2417-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
2419+
$(SPEC_RUNNABLE_EXAMPLE_RUN
24182420
---
24192421
int sum(int[] ar ...) // typesafe variadic function
24202422
{
@@ -2424,30 +2426,22 @@ $(H4 $(LNAME2 typesafe_variadic_functions, Typesafe Variadic Functions))
24242426
return s;
24252427
}
24262428

2427-
import std.stdio;
2428-
24292429
void main()
24302430
{
2431-
writeln(stan()); // 6
2432-
writeln(ollie()); // 15
2433-
}
2434-
2435-
int stan()
2436-
{
2437-
return sum(1, 2, 3) + sum(); // returns 6+0
2438-
}
2431+
assert(sum(1, 2, 3) == 6);
2432+
assert(sum() == 0);
24392433

2440-
int ollie()
2441-
{
24422434
int[3] ii = [4, 5, 6];
2443-
return sum(ii); // returns 15
2435+
assert(sum(ii) == 15);
24442436
}
24452437
---
24462438
)
24472439

2448-
$(P For static arrays, the number of arguments must
2449-
match the array dimension.)
2440+
$(P A static array variadic parameter accepts either a static
2441+
array argument or a fixed number of arguments which matches
2442+
the array dimension:)
24502443

2444+
$(SPEC_RUNNABLE_EXAMPLE_RUN
24512445
---
24522446
int sum(int[3] ar ...) // typesafe variadic function
24532447
{
@@ -2457,20 +2451,22 @@ $(H4 $(LNAME2 typesafe_variadic_functions, Typesafe Variadic Functions))
24572451
return s;
24582452
}
24592453

2460-
int frank()
2454+
void main()
24612455
{
2462-
return sum(2, 3); // error, need 3 values for array
2463-
return sum(1, 2, 3); // returns 6
2464-
}
2456+
int i;
2457+
//i = sum(2, 3); // error, need 3 values for array
2458+
i = sum(1, 2, 3);
2459+
assert(i == 6);
24652460

2466-
int dave()
2467-
{
24682461
int[3] ii = [4, 5, 6];
2462+
i = sum(ii);
2463+
assert(i == 15);
2464+
24692465
int[] jj = ii;
2470-
return sum(ii); // returns 15
2471-
return sum(jj); // error, type mismatch
2466+
//i = sum(jj); // error, type mismatch
24722467
}
24732468
---
2469+
)
24742470

24752471
$(DDOC_DEPRECATED For class objects:)
24762472

@@ -2522,7 +2518,8 @@ $(H4 $(LNAME2 typesafe_variadic_functions, Typesafe Variadic Functions))
25222518
$(IMPLEMENTATION_DEFINED the variadic object or array instance
25232519
may be constructed on the stack.)
25242520

2525-
$(P For other types, the argument is passed by value.)
2521+
$(P For other types, the variadic parameter matches exactly one
2522+
argument, which is passed by value.)
25262523

25272524
---
25282525
int neil(int i ...)
@@ -2532,6 +2529,7 @@ $(H4 $(LNAME2 typesafe_variadic_functions, Typesafe Variadic Functions))
25322529

25332530
void buzz()
25342531
{
2532+
neil(); // error, missing argument
25352533
neil(3); // returns 3
25362534
neil(3, 4); // error, too many arguments
25372535
int[] x;

0 commit comments

Comments
 (0)