PageRenderTime 31ms CodeModel.GetById 14ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Examples/d/funcptr/d1/runme.d

#
D | 42 lines | 29 code | 8 blank | 5 comment | 1 complexity | 34d3c018ac76a8ace26c150775549c54 MD5 | raw file
 1module runme;
 2
 3import tango.io.Stdout;
 4static import example;
 5
 6extern(C) int add(int a, int b) {
 7  return a + b;
 8}
 9
10extern(C) int sub(int a, int b) {
11  return a - b;
12}
13
14extern(C) int mul(int a, int b) {
15  return a * b;
16}
17
18void main() {
19  int a = 37;
20  int b = 42;
21
22  Stdout( "a = " )( a ).newline;
23  Stdout( "b = " )( b ).newline;
24
25  Stdout( "Trying some C callback functions:" ).newline;
26  Stdout( "    ADD(a,b) = " )( example.do_op( a, b, example.ADD ) ).newline;
27  Stdout( "    SUB(a,b) = " )( example.do_op( a, b, example.SUB ) ).newline;
28  Stdout( "    MUL(a,b) = " )( example.do_op( a, b, example.MUL ) ).newline;
29
30  version (LDC) {
31    // Currently, there is no way to specify the calling convention for
32    // function pointer parameters in D, but LDC does strict typechecking for
33    // them (which is reasonable, but not covered by the language spec yet).
34    // As a result, there is no way to make the code below compile with LDC at
35    // the moment, so just skip it.
36  } else {
37    Stdout( "Now the same with callback functions defined in D:" ).newline;
38    Stdout( "    add(a,b) = " )( example.do_op( a, b, &add ) ).newline;
39    Stdout( "    sub(a,b) = " )( example.do_op( a, b, &sub ) ).newline;
40    Stdout( "    mul(a,b) = " )( example.do_op( a, b, &mul ) ).newline;
41  }
42}