Anonymous user
Higher-order functions: Difference between revisions
Updated both D versions
(Updated both D versions) |
|||
Line 523:
=={{header|D}}==
D's function/delegate will be investigated by passing each type of function/delegate to _test_ as argument.▼
return f(a, b);▼
}▼
import std.stdio ;▼
// test the function argument▼
}</lang>▼
{{out}}
<pre>Add: 5
Multiply: 6</pre>
▲
string test(U)(string scopes, U func) {
string typeStr = typeid(typeof(func)).toString
string isFunc = (typeStr[$ - 1] == '*') ? "function" : "delegate"
writefln("Hi, %-13s : scope: %-8s (%s) : %s",
return scopes ;▼
func(), scopes, isFunc, typeStr );
}
//
string aFunction() { return "Function"
// Implicit-Function-Template-Instantiation (IFTI)
T tmpFunc(T)() { return "IFTI.function"
// Member in a template.
template tmpGroup(T) {
T t0(){ return "Tmp.member.0"
T t1(){ return "Tmp.member.1"
T t2(){ return "Tmp.member.2"
}
//
template Impl() {
static string aStatic() { return "Static Method"
string aMethod() { return "Method"
}
class C { mixin Impl!() ; }▼
struct S { mixin Impl!() ; }▼
▲void main() {
// nested function▼
string aNested(){▼
return "Nested" ;▼
} ▼
auto variableF = function string() { return "variable.F"; } ;▼
▲ string aNested() {
auto variableD = delegate string() { return "variable.D"; } ;▼
S s ;▼
C c = new C;
"Class".test(&C.aStatic) ▼
"
.test(&tmpGroup!(string).t2);
// leteral function/delegate▼
"Literal".test(function string() { return "literal.F"; })▼
.test(
▲ "Literal".test(function string() { return "literal.F"; })
.test(delegate string() { return "literal.D"; });
}</lang>
{{out}}}
<pre>Hi, Function : scope: Global (function) : immutable(char)[]()*
Hi, Nested : scope: Nested (delegate) : immutable(char)[] delegate()
Hi, Static Method : scope: Class (function) : immutable(char)[]()*
Hi, Method : scope: Class (delegate) : immutable(char)[] delegate()
Hi, Static Method : scope: Struct (function) : immutable(char)[]()*
▲import std.stdio, std.functional;
Hi, Method : scope: Struct (delegate) : immutable(char)[] delegate()
Hi, IFTI.function : scope: Template (function) : immutable(char)[]()*
▲int someprocedure (int a, int b, int delegate (int, int) f)
Hi, Tmp.member.2 : scope: Template (function) : immutable(char)[]()*
Hi, variable.F : scope: Binding (function) : immutable(char)[]()*
▲ return f(a, b);
Hi, variable.D : scope: Binding (delegate) : immutable(char)[] delegate()
▲}
Hi, literal.F : scope: Literal (function) : immutable(char)[]()*
Hi, literal.D : scope: Literal (delegate) : immutable(char)[] delegate()</pre>
▲void main ()
▲ writeln("add: ", someprocedure(2, 3, (int a, int b) { return a + b; } ));
▲ writeln("multiply: ", someprocedure(2, 3, (int a, int b) { return a * b; } ));
▲</lang>
=={{header|Delphi}}==
|