forked from DlangRen/Programming-in-D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.d
1196 lines (899 loc) · 34.3 KB
/
lambda.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Ddoc
$(DERS_BOLUMU Function Pointers, Delegates, and Lambdas)
$(P
Function pointers are for storing addresses of functions in order to execute those functions at a later time. Function pointers are similar to their counterparts in the C programming language.
)
$(P
Delegates store both a function pointer and the context to execute that function pointer in. The stored context can either be the scope that the function execution will take place or a $(C struct) or $(C class) object.
)
$(P
Delegates enable $(I closures) as well, a concept that is supported by most functional programming languages.
)
$(H5 $(IX function pointer) $(IX pointer, function) Function pointers)
$(P
$(IX &, function address) We have seen in the previous chapter that it is possible to take addresses of functions with the $(C &) operator. In one of those examples, we passed such an address to a function template.
)
$(P
Taking advantage of the fact that template type parameters can match any type, let's pass a function pointer to a template to observe its type by printing its $(C .stringof) property:
)
---
import std.stdio;
int myFunction(char c, double d) {
return 42;
}
void main() {
myTemplate($(HILITE &myFunction)); // Taking the function's address and
// passing it as a parameter
}
void myTemplate(T)(T parameter) {
writeln("type : ", T.stringof);
writeln("value: ", parameter);
}
---
$(P
The output of the program reveals the type and the address of $(C myFunction()):
)
$(SHELL
type : int function(char c, double d)
value: 406948
)
$(H6 $(IX member function pointer) $(IX pointer, member function) Member function pointers)
$(P
The address of a member function can be taken either on a type or on an object of a type, with different results:
)
---
struct MyStruct {
void func() {
}
}
void main() {
auto o = MyStruct();
auto f = &$(HILITE MyStruct).func; // on a type
auto d = &$(HILITE o).func; // on an object
static assert(is (typeof($(HILITE f)) == void $(HILITE function)()));
static assert(is (typeof($(HILITE d)) == void $(HILITE delegate)()));
}
---
$(P
As the two $(C static assert) lines above indicate, $(C f) is a $(C function) and $(C d) is a $(C delegate). We will see later below that $(C d) can be called directly but $(C f) needs an object to be called on.
)
$(H6 Definition)
$(P
$(IX function) Similar to regular pointers, each function pointer type can point exactly to a particular type of function; the parameter list and the return type of the function pointer and the function must match. Function pointers are defined by the $(C function) keyword between the return type and the parameter list of that particular type:
)
---
$(I return_type) function($(I parameters)) ptr;
---
$(P
The names of the parameters ($(C c) and $(C d) in the output above) are optional. Because $(C myFunction()) takes a $(C char) and a $(C double) and returns an $(C int), the type of a function pointer that can point at $(C myFunction()) must be defined accordingly:
)
---
int function(char, double) ptr = &myFunction;
---
$(P
The line above defines $(C ptr) as a function pointer taking two parameters ($(C char) and $(C double)) and returning $(C int). Its value is the address of $(C myFunction()).
)
$(P
Function pointer syntax is relatively harder to read; it is common to make code more readable by an $(C alias):
)
---
alias CalculationFunc = int function(char, double);
---
$(P
That alias makes the code easier to read:
)
---
CalculationFunc ptr = &myFunction;
---
$(P
As with any type, $(C auto) can be used as well:
)
---
auto ptr = &myFunction;
---
$(H6 Calling a function pointer)
$(P
Function pointers can be called exactly like functions:
)
---
int result = $(HILITE ptr)('a', 5.67);
assert(result == 42);
---
$(P
The call $(C ptr('a', 5.67)) above is the equivalent of calling the actual function by $(C myFunction('a', 5.67)).
)
$(H6 When to use)
$(P
Because function pointers store what function to call and they are called exactly like the functions that they point at, function pointers effectively store the behavior of the program for later.
)
$(P
There are many other features of D that are about program behavior. For example, the appropriate function to call to calculate the wages of an $(C Employee) can be determined by the value of an $(C enum) member:
)
---
final switch (employee.type) {
case EmployeeType.fullTime:
fullTimeEmployeeWages();
break;
case EmployeeType.hourly:
hourlyEmployeeWages();
break;
}
---
$(P
Unfortunately, that method is relatively harder to maintain because it obviously has to support all known employee types. If a new type of employee is added to the program, then all such $(C switch) statements must be located so that a new $(C case) clause is added for the new employee type.
)
$(P
A more common alternative of implementing behavior differences is polymorphism. An $(C Employee) interface can be defined and different wage calculations can be handled by different implementations of that interface:
)
---
interface Employee {
double wages();
}
class FullTimeEmployee : Employee {
double wages() {
double result;
// ...
return result;
}
}
class HourlyEmployee : Employee {
double wages() {
double result;
// ...
return result;
}
}
// ...
double result = employee.wages();
---
$(P
Function pointers are yet another alternative for implementing different behavior. They are more common in programming languages that do not support object oriented programming.
)
$(H6 Function pointer as a parameter)
$(P
Let's design a function that takes an array and returns another array. This function will filter out elements with values less than or equal to zero, and multiply the others by ten:
)
---
$(CODE_NAME filterAndConvert)int[] filterAndConvert(const int[] numbers) {
int[] result;
foreach (e; numbers) {
if (e > 0) { // filtering,
immutable newNumber = e * 10; // and conversion
result ~= newNumber;
}
}
return result;
}
---
$(P
The following program demonstrates its behavior with randomly generated values:
)
---
$(CODE_XREF filterAndConvert)import std.stdio;
import std.random;
void main() {
int[] numbers;
// Random numbers
foreach (i; 0 .. 10) {
numbers ~= uniform(0, 10) - 5;
}
writeln("input : ", numbers);
writeln("output: ", filterAndConvert(numbers));
}
---
$(P
The output contains numbers that are ten times the original numbers, which were greater than zero to begin with. The original numbers that have been selected are highlighted:
)
$(SHELL
input : [-2, $(HILITE 2), -2, $(HILITE 3), -2, $(HILITE 2), -1, -4, 0, 0]
output: [20, 30, 20]
)
$(P
$(C filterAndConvert()) is for a very specific task: It always selects numbers that are greater than zero and always multiplies them by ten. It could be more useful if the behaviors of filtering and conversion were parameterized.
)
$(P
Noting that filtering is a form of conversion as well (from $(C int) to $(C bool)), $(C filterAndConvert()) performs two conversions:
)
$(UL
$(LI $(C number > 0), which produces $(C bool) by considering an $(C int) value.)
$(LI $(C number * 10), which produces $(C int) from an $(C int) value.)
)
$(P
Let's define convenient aliases for function pointers that would match the two conversions above:
)
---
alias Predicate = bool function(int); // makes bool from int
alias Convertor = int function(int); // makes int from int
---
$(P
$(C Predicate) is the type of functions that take $(C int) and return $(C bool), and $(C Convertor) is the type of functions that take $(C int) and return $(C int).
)
$(P
If we provide such function pointers as parameters, we can have $(C filterAndConvert()) use those function pointers during its work:
)
---
int[] filterAndConvert(const int[] numbers,
$(HILITE Predicate predicate),
$(HILITE Convertor convertor)) {
int[] result;
foreach (number; numbers) {
if ($(HILITE predicate(number))) {
immutable newNumber = $(HILITE convertor(number));
result ~= newNumber;
}
}
return result;
}
---
$(P
$(C filterAndConvert()) is now an algorithm that is independent of the actual filtering and conversion operations. When desired, its earlier behavior can be achieved by the following two simple functions:
)
---
bool isGreaterThanZero(int number) {
return number > 0;
}
int tenTimes(int number) {
return number * 10;
}
// ...
writeln("output: ", filterAndConvert(numbers,
$(HILITE &isGreaterThanZero),
$(HILITE &tenTimes)));
---
$(P
This design allows calling $(C filterAndConvert()) with any filtering and conversion behaviors. For example, the following two functions would make $(C filterAndConvert()) produce $(I the negatives of the even numbers):
)
---
bool isEven(int number) {
return (number % 2) == 0;
}
int negativeOf(int number) {
return -number;
}
// ...
writeln("output: ", filterAndConvert(numbers,
&isEven,
&negativeOf));
---
$(P
The output:
)
$(SHELL
input : [3, -3, 2, 1, -5, 1, 2, 3, 4, -4]
output: [-2, -2, -4, 4]
)
$(P
As seen in these examples, sometimes such functions are so trivial that defining them as proper functions with name, return type, parameter list, and curly brackets is unnecessarily wordy.
)
$(P
As we will see below, the $(C =>) syntax of anonymous functions makes the code more concise and more readable. The following line has anonymous functions that are the equivalents of $(C isEven()) and $(C negativeOf()), without proper function definitions:
)
---
writeln("output: ", filterAndConvert(numbers,
number => (number % 2) == 0,
number => -number));
---
$(H6 Function pointer as a member)
$(P
Function pointers can be stored as members of structs and classes as well. To see this, let's design a $(C class) that takes the predicate and convertor as constructor parameters in order to use them later on:
)
---
class NumberHandler {
$(HILITE Predicate predicate);
$(HILITE Convertor convertor);
this(Predicate predicate, Convertor convertor) {
this.predicate = predicate;
this.convertor = convertor;
}
int[] handle(const int[] numbers) {
int[] result;
foreach (number; numbers) {
if (predicate(number)) {
immutable newNumber = convertor(number);
result ~= newNumber;
}
}
return result;
}
}
---
$(P
An object of that type can be used similarly to $(C filterAndConvert()):
)
---
auto handler = new NumberHandler($(HILITE &isEven), $(HILITE &negativeOf));
writeln("result: ", handler.handle(numbers));
---
$(H5 $(IX anonymous function) $(IX function, anonymous) $(IX function, lambda) $(IX function literal) $(IX literal, function) $(IX lambda) Anonymous functions)
$(P
The code can be more readable and concise when short functions are defined without proper function definitions.
)
$(P
Anonymous functions, which are also knows as $(I function literals) or $(I lambdas), allow defining functions inside of expressions. Anonymous functions can be used at any point where a function pointer can be used.
)
$(P
We will get to their shorter $(C =>) syntax later below. Let's first see their full syntax, which is usually too wordy especially when it appears inside of other expressions:
)
---
function $(I return_type)($(I parameters)) { /* operations */ }
---
$(P
For example, an object of $(C NumberHandler) that produces $(I 7 times the numbers that are greater than 2) can be constructed by anonymous functions as in the following code:
)
---
new NumberHandler(function bool(int number) { return number > 2; },
function int(int number) { return number * 7; });
---
$(P
Two advantages of the code above is that the functions are not defined as proper functions and that their implementations are visible right where the $(C NumberHandler) object is constructed.
)
$(P
Note that the anonymous function syntax is very similar to regular function syntax. Although this consistency has benefits, the full syntax of anonymous functions makes code too wordy.
)
$(P
For that reason, there are various shorter ways of defining anonymous functions.
)
$(H6 Shorter syntax)
$(P
When the return type can be deduced from the $(C return) statement inside the anonymous function, then the return type need not be specified (The place where the return type would normally appear is highlighted by code comments.):
)
---
new NumberHandler(function /**/(int number) { return number > 2; },
function /**/(int number) { return number * 7; });
---
$(P
Further, when the anonymous function does not take parameters, its parameter list need not be provided. Let's consider a function that takes a function pointer that takes $(I nothing) and returns $(C double):
)
---
void foo(double function$(HILITE ()) func) {
// ...
}
---
$(P
Anonymous functions that are passed to that function need not have the empty parameter list. Therefore, all three of the following anonymous function syntaxes are equivalent:
)
---
foo(function double() { return 42.42; });
foo(function () { return 42.42; });
foo(function { return 42.42; });
---
$(P
The first one is written in the full syntax. The second one omits the return type, taking advantage of the return type deduction. The third one omits the unnecessary parameter list.
)
$(P
Even further, the keyword $(C function) need not be provided either. In that case it is left to the compiler to determine whether it is an anonymous function or an anonymous delegate. Unless it uses a variable from one of the enclosing scopes, it is a function:
)
---
foo({ return 42.42; });
---
$(P
Most anonymous functions can be defined even shorter by the $(I lambda syntax).
)
$(H6 $(IX =>) Lambda syntax instead of a single $(C return) statement)
$(P
In most cases even the shortest syntax above is unnecessarily cluttered. The curly brackets that are just inside the function parameter list make the code harder to read and a $(C return) statement as well as its semicolon inside a function argument looks out of place.
)
$(P
Let's start with the full syntax of an anonymous function that has a single $(C return) statement:
)
---
function $(I return_type)($(I parameters)) { return $(I expression); }
---
$(P
We have already seen that the $(C function) keyword is not necessary and the return type can be deduced:
)
---
($(I parameters)) { return $(I expression); }
---
$(P
The equivalent of that definition is the following $(C =>) syntax, where the $(C =>) characters replace the curly brackets, the $(C return) keyword, and the semicolon:
)
---
($(I parameters)) => $(I expression)
---
$(P
The meaning of that syntax can be spelled out as "given those parameters, produce this expression (value)".
)
$(P
Further, when there is a single parameter, the parentheses around the parameter list can be omitted as well:
)
---
$(I single_parameter) => $(I expression)
---
$(P
On the other hand, to avoid grammar ambiguities, the parameter list must still be written as empty parentheses when there is no parameter at all:
)
---
() => $(I expression)
---
$(P
Programmers who know lambdas from other languages may make a mistake of using curly brackets after the $(C =>) characters, which can be valid D syntax with a different meaning:
)
---
// A lambda that returns 'a + 1'
auto l0 = (int a) => a + 1
// A lambda that returns a parameter-less lambda that
// returns 'a + 1'
auto l1 = (int a) => $(HILITE {) return a + 1; $(HILITE })
assert(l0(42) == 43);
assert(l1(42)$(HILITE ()) == 43); // Executing what l1 returns
---
$(P
$(IX filter, std.algorithm) Let's use the lambda syntax in a predicate passed to $(C std.algorithm.filter). $(C filter()) takes a predicate as its template parameter and a range as its function parameter. It applies the predicate to each element of the range and returns the ones that satisfy the predicate. One of several ways of specifying the predicate is the lambda syntax.
)
$(P
($(I Note: We will see ranges in a later chapter. At this point, it should be sufficient to know that D slices are ranges.))
)
$(P
The following lambda is a predicate that matches elements that are greater than 10:
)
---
import std.stdio;
import std.algorithm;
void main() {
int[] numbers = [ 20, 1, 10, 300, -2 ];
writeln(numbers.filter!($(HILITE number => number > 10)));
}
---
$(P
The output contains only the elements that satisfy the predicate:
)
$(SHELL
[20, 300]
)
$(P
For comparison, let's write the same lambda in the longest syntax. The curly brackets that define the body of the anonymous function are highlighted:
)
---
writeln(numbers.filter!(function bool(int number) $(HILITE {)
return number > 10;
$(HILITE })));
---
$(P
As another example, this time let's define an anonymous function that takes two parameters. The following algorithm takes two slices and passes their corresponding elements one by one to a $(C function) that itself takes two parameters. It then collects and returns the results as another slice:
)
---
$(CODE_NAME binaryAlgorithm)import std.exception;
int[] binaryAlgorithm(int function$(HILITE (int, int)) func,
const int[] slice1,
const int[] slice2) {
enforce(slice1.length == slice2.length);
int[] results;
foreach (i; 0 .. slice1.length) {
results ~= func(slice1[i], slice2[i]);
}
return results;
}
---
$(P
Since the $(C function) parameter above takes two parameters, lambdas that can be passed to $(C binaryAlgorithm()) must take two parameters as well:
)
---
$(CODE_XREF binaryAlgorithm)import std.stdio;
void main() {
writeln(binaryAlgorithm($(HILITE (a, b)) => (a * 10) + b,
[ 1, 2, 3 ],
[ 4, 5, 6 ]));
}
---
$(P
The output contains ten times the elements of the first array plus the elements of the second array (e.g. 14 is 10 * 1 + 4):
)
$(SHELL
[14, 25, 36]
)
$(H5 $(IX delegate) Delegates)
$(P
$(IX context) $(IX closure) A delegate is a combination of a function pointer and the context that it should be executed in. Delegates also support $(I closures) in D. Closures are a feature supported by many functional programming languages.
)
$(P
As we have seen in $(LINK2 /ders/d.en/lifetimes.html, the Lifetimes and Fundamental Operations chapter), the lifetime of a variable ends upon leaving the scope that it is defined in:
)
---
{
int increment = 10;
// ...
} // ← the life of 'increment' ends here
---
$(P
That is why the address of such a local variable cannot be returned from a function.
)
$(P
Let's imagine that $(C increment) is a local variable of a function that itself returns a $(C function). Let's make it so that the returned lambda happens to use that local variable:
)
---
alias Calculator = int function(int);
Calculator makeCalculator() {
int increment = 10;
return value => $(HILITE increment) + value; $(DERLEME_HATASI)
}
---
$(P
That code is in error because the returned lambda makes use of a local variable that is about to go out of scope. If the code were allowed to compile, the lambda would be trying to access $(C increment), whose life has already ended.
)
$(P
For that code to be compiled and work correctly, the lifetime of $(C increment) must at least be as long as the lifetime of the lambda that uses it. Delegates extend the lifetime of the context of a lambda so that the local state that the function uses remains valid.
)
$(P
$(C delegate) syntax is similar to $(C function) syntax, the only difference being the keyword. That change is sufficient to make the previous code compile:
)
---
alias Calculator = int $(HILITE delegate)(int);
Calculator makeCalculator() {
int increment = 10;
return value => increment + value;
}
---
$(P
Having been used by a delegate, the local variable $(C increment) will now live as long as that delegate lives. The variable is available to the delegate just as any other variable would be, mutable as needed. We will see examples of this in the next chapter when using delegates with $(C opApply()) member functions.
)
$(P
The following is a test of the delegate above:
)
---
auto calculator = makeCalculator();
writeln("The result of the calculation: ", calculator(3));
---
$(P
Note that $(C makeCalculator()) returns an anonymous delegate. The code above assigns that delegate to the variable $(C calculator) and then calls it by $(C calculator(3)). Since the delegate is implemented to return the sum of its parameter and the variable $(C increment), the code outputs the sum of 3 and 10:
)
$(SHELL
The result of the calculation: 13
)
$(H6 Shorter syntax)
$(P
As we have already used in the previous example, delegates can take advantage of the shorter syntaxes as well. When neither $(C function) nor $(C delegate) is specified, the type of the lambda is decided by the compiler, depending on whether the lambda accesses local state. If so, then it is a $(C delegate).
)
$(P
The following example has a delegate that does not take any parameters:
)
---
int[] delimitedNumbers(int count, int delegate$(HILITE ()) numberGenerator) {
int[] result = [ -1 ];
result.reserve(count + 2);
foreach (i; 0 .. count) {
result ~= numberGenerator();
}
result ~= -1;
return result;
}
---
$(P
The function $(C delimitedNumbers()) generates a slice where the first and last elements are -1. It takes two parameters that specify the other elements that come between those first and last elements.
)
$(P
Let's call that function with a trivial delegate that always returns the same value. Remember that when there is no parameter, the parameter list of a lambda must be specified as empty:
)
---
writeln(delimitedNumbers(3, $(HILITE () => 42)));
---
$(P
The output:
)
$(SHELL
-1 42 42 42 -1
)
$(P
Let's call $(C delimitedNumbers()) this time with a delegate that makes use of a local variable:
)
---
int lastNumber;
writeln(delimitedNumbers(
15, $(HILITE () => lastNumber += uniform(0, 3))));
writeln("Last number: ", lastNumber);
---
$(P
Although that delegate produces a random value, since the value is added to the last one, none of the generated values is less than its predecessor:
)
$(SHELL
-1 0 2 3 4 6 6 8 9 9 9 10 12 14 15 17 -1
Last number: 17
)
$(H6 $(IX &, object delegate) $(IX delegate, member function) $(IX object delegate) $(IX member function delegate) An object and a member function as a delegate)
$(P
We have seen that a delegate is nothing but a function pointer and the context that it is to be executed in. Instead of those two, a delegate can also be composed of a member function and an existing object that that member function is to be called on.
)
$(P
The syntax that defines such a delegate from an object is the following:
)
---
&$(I object).$(I member_function)
---
$(P
Let's first observe that such a syntax indeed defines a $(C delegate) by printing its $(C string) representation:
)
---
import std.stdio;
struct Location {
long x;
long y;
void moveHorizontally(long step) { x += step; }
void moveVertically(long step) { y += step; }
}
void main() {
auto location = Location();
writeln(typeof($(HILITE &location.moveHorizontally)).stringof);
}
---
$(P
According to the output, the type of $(C moveHorizontally()) called on $(C location) is indeed a $(C delegate):
)
$(SHELL
void delegate(long step)
)
$(P
Note that the $(C &) syntax is only for constructing the delegate. The delegate will be called later by the function call syntax:
)
---
// The definition of the delegate variable:
auto directionFunction = &location.moveHorizontally;
// Calling the delegate by the function call syntax:
directionFunction$(HILITE (3));
writeln(location);
---
$(P
Since the $(C delegate) combines the $(C location) object and the $(C moveHorizontally()) member function, calling the delegate is the equivalent of calling $(C moveHorizontally()) on $(C location). The output indicates that the object has indeed moved 3 steps horizontally:
)
$(SHELL
Location(3, 0)
)
$(P
Function pointers, lambdas, and delegates are expressions. They can be used in places where a value of their type is expected. For example, a slice of $(C delegate) objects is initialized below from delegates constructed from an object and its various member functions. The $(C delegate) elements of the slice are later called just like functions:
)
---
auto location = Location();
void delegate(long)[] movements =
[ &location.moveHorizontally,
&location.moveVertically,
&location.moveHorizontally ];
foreach (movement; movements) {
movement$(HILITE (1));
}
writeln(location);
---
$(P
According to the elements of the slice, the location has been changed twice horizontally and once vertically:
)
$(SHELL
Location(2, 1)
)
$(H6 $(IX .ptr, delegate context) $(IX pointer, delegate context) $(IX .funcptr) $(IX function pointer, delegate) Delegate properties)
$(P
The function and context pointers of a delegate can be accessed through its $(C .funcptr) and $(C .ptr) properties, respectively:
)
---
struct MyStruct {
void func() {
}
}
void main() {
auto o = MyStruct();
auto d = &o.func;
assert(d$(HILITE .funcptr) == &MyStruct.func);
assert(d$(HILITE .ptr) == &o);
}
---
$(P
It is possible to make a $(C delegate) from scratch by setting those properties explicitly:
)
---
struct MyStruct {
int i;
void func() {
import std.stdio;
writeln(i);
}
}
void main() {
auto o = MyStruct(42);
void delegate() d;
assert(d is null); // null to begin with
d$(HILITE .funcptr) = &MyStruct.func;
d$(HILITE .ptr) = &o;
$(HILITE d());
}
---
$(P
Calling the delegate above as $(C d()) is the equivalent of the expression $(C o.func()) (i.e. calling $(C MyStruct.func) on $(C o)):
)
$(SHELL
42
)
$(H6 $(IX lazy parameter as delegate) Lazy parameters are delegates)
$(P
We saw the $(C lazy) keyword in $(LINK2 /ders/d.en/function_parameters.html, the Function Parameters chapter):
)
---
void log(Level level, $(HILITE lazy) string message) {
if (level >= interestedLevel) {
writefln("%s", message);
}
}
// ...
if (failedToConnect) {
log(Level.medium,
$(HILITE format)("Failure. The connection state is '%s'.",
getConnectionState()));
}
---
$(P
Because $(C message) is a $(C lazy) parameter above, the entire $(C format) expression (including the $(C getConnectionState()) call that it makes) would be evaluated if and when that parameter is used inside $(C log()).
)
$(P
Behind the scenes, lazy parameters are in fact delegates and the arguments that are passed to lazy parameters are delegate objects that are created automatically by the compiler. The code below is the equivalent of the one above:
)
---
void log(Level level, string $(HILITE delegate)() lazyMessage) { // (1)
if (level >= interestedLevel) {
writefln("%s", $(HILITE lazyMessage())); // (2)
}
}
// ...
if (failedToConnect) {
log(Level.medium,
delegate string() $(HILITE {) // (3)
return format(
"Failure. The connection state is '%s'.",
getConnectionState());
$(HILITE }));
}
---
$(OL
$(LI The $(C lazy) parameter is not a $(C string) but a delegate that returns a $(C string).)
$(LI The delegate is called to get its return value.)
$(LI The entire expression is wrapped inside a delegate and returned from it.)
)
$(H6 $(IX lazy variadic functions) $(IX variadic function, lazy) Lazy variadic functions)
$(P
When a function needs a variable number of lazy parameters, it is necessarily impossible to specify those $(I unknown number of) parameters as $(C lazy).
)
$(P
The solution is to use variadic $(C delegate) parameters. Such parameters can receive any number of expressions that are the same as the $(I return type) of those delegates. The delegates cannot take parameters:
)
---