Skip to content

Commit 920d92c

Browse files
authored
[spec/template] Improve type sequence deduction docs (#4318)
Show inferred template parameter types. Mention *deduced from the parameter list*. Add simpler example. Simplify `partial` example.
1 parent 575da9e commit 920d92c

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

spec/template.dd

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,10 @@ $(H4 $(LNAME2 typeseq_deduction, Type Sequence Deduction))
10721072

10731073
void main()
10741074
{
1075+
// Calls `print` with:
1076+
// T = int, Args = (char, double)
1077+
// T = char, Args = (double)
1078+
// T = double, Args = ()
10751079
print(1, 'a', 6.8);
10761080
}
10771081
---
@@ -1085,38 +1089,46 @@ a
10851089
6.8
10861090
)
10871091

1088-
$(P Type sequences can also be deduced from the type of a delegate
1089-
or function parameter list passed as a function argument:)
1092+
$(P Type sequences can also be deduced from the parameter list of a
1093+
$(DDSUBLINK spec/function, function-pointers-delegates, function pointer or delegate) passed as a function argument:)
1094+
1095+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1096+
---
1097+
size_t arity(R, Args...)(R function(Args) fp) => Args.length;
1098+
1099+
int f(int);
1100+
void g(string, Object);
1101+
1102+
static assert(arity((){}) == 0); // R = void, Args = ()
1103+
static assert(arity(&f) == 1); // R = int, Args = (int)
1104+
static assert(arity(&g) == 2); // R = void, Args = (string, Object)
1105+
---
1106+
)
1107+
$(P Deduction can partially match a parameter list:)
10901108

10911109
$(SPEC_RUNNABLE_EXAMPLE_RUN
10921110
----
1093-
import std.stdio;
1094-
10951111
/* Partially applies a delegate by tying its first argument to a particular value.
10961112
* R = return type
10971113
* T = first argument type
10981114
* Args = TypeSeq of remaining argument types
10991115
*/
1100-
R delegate(Args) partial(R, T, Args...)(R delegate(T, Args) dg, T first)
1116+
R delegate(Args) partial(R, T, Args...)(R function(T, Args) dg, T first)
11011117
{
11021118
// return a closure
11031119
return (Args args) => dg(first, args);
11041120
}
11051121

1122+
int plus(int x, int y, int z) => x + y + z;
1123+
11061124
void main()
11071125
{
1108-
int plus(int x, int y, int z)
1109-
{
1110-
return x + y + z;
1111-
}
1112-
1113-
import std.stdio;
1114-
auto plus_two = partial(&plus, 2);
1115-
writeln(plus_two(6, 8)); // prints 16
1126+
auto plus_two = partial(&plus, 2); // R = int, T = int, Args = (int, int)
1127+
assert(plus_two(6, 8) == 16);
11161128
}
11171129
----
11181130
)
1119-
See also: $(REF partial, std,functional)
1131+
$(P See also: $(REF partial, std,functional).)
11201132

11211133
$(H4 $(LNAME2 variadic_template_specialization, Specialization))
11221134

@@ -1366,6 +1378,9 @@ $(H4 $(LNAME2 ifti-restrictions, Restrictions))
13661378
---
13671379
)
13681380

1381+
$(P A *TypeSeq* template parameter $(RELATIVE_LINK2 typeseq_deduction, can be deduced)
1382+
from function arguments.)
1383+
13691384
$(H4 $(LNAME2 ifti-conversions, Type Conversions))
13701385

13711386
$(P If template type parameters match the literal expressions on function arguments,

0 commit comments

Comments
 (0)