@@ -1381,13 +1381,13 @@ $(GNAME Lambda):
1381
1381
The first form is equivalent to:
1382
1382
)
1383
1383
---
1384
- delegate ( $(IDENTIFIER) ) { return $(I AssignExpression); }
1384
+ ( $(IDENTIFIER) ) { return $(I AssignExpression); }
1385
1385
---
1386
1386
1387
1387
$(P And the second:)
1388
1388
1389
1389
---
1390
- delegate $(I ParameterAttributes) { return $(I AssignExpression); }
1390
+ $(I ParameterAttributes) { return $(I AssignExpression); }
1391
1391
---
1392
1392
1393
1393
Example usage:
@@ -1431,7 +1431,8 @@ $(V2 $(GLINK2 declaration, Parameters) $(GLINK2 declaration, FunctionAttrib
1431
1431
The type of a function literal is pointer to function or
1432
1432
pointer to delegate.
1433
1433
If the keywords $(B function) or $(B delegate) are omitted,
1434
- it defaults to being a delegate.)
1434
+ it is inferred from whether $(I FunctionBody) is actually
1435
+ accessing to the outer context.)
1435
1436
1436
1437
$(P For example:)
1437
1438
@@ -1480,16 +1481,36 @@ void test() {
1480
1481
}
1481
1482
-------------
1482
1483
1483
- $(P and the following where the return type $(B int) is
1484
- inferred:)
1484
+ $(P and the following where the return type $(B int) and
1485
+ $(B function)/$(B delegate) are inferred:)
1485
1486
1486
1487
-------------
1487
1488
int abc(int delegate(long i));
1489
+ int def(int function(long s));
1488
1490
1489
1491
void test() {
1490
1492
int b = 3;
1491
1493
1492
- abc( $(B (long c) { return 6 + b; }) );
1494
+ abc( $(B (long c) { return 6 + b; }) ); // inferred to $(B delegate)
1495
+ def( $(B (long c) { return c * 2; }) ); // inferred to $(B function)
1496
+ //def( $(B (long c) { return c * b; }) ); // error!
1497
+ // Because the FunctionBody accesses b, then the function literal type
1498
+ // is inferred to delegate. But def cannot receive delegate.
1499
+ }
1500
+ -------------
1501
+
1502
+ If the type of a function literal can be uniquely determined from its context,
1503
+ the parameter type inference is possible.
1504
+
1505
+ -------------
1506
+ void foo(int function(int) fp);
1507
+
1508
+ void test() {
1509
+ int function(int) fp = (n) { return n * 2; };
1510
+ // The type of parameter n is inferred to $(B int).
1511
+
1512
+ foo((n) { return n * 2; });
1513
+ // The type of parameter n is inferred to $(B int).
1493
1514
}
1494
1515
-------------
1495
1516
0 commit comments