テンプレート・ミックスイン

temp_mixin.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
import std.stdio;

mixin template Part(T){
  T add(T lhs, T rhs){
    return lhs + rhs;
  }
}

mixin template Value(){
  int value;
}

class Foo{
  mixin Part!(int);
  mixin Value;
}

void main(){
  Foo x = new Foo;

  writeln( x.add(10, 20) );

  x.value = 100;
  writeln( x.value );
}

temp_mixin.d の実行結果は:

[cactus:~/code_d/d_tuts]% ./temp_mixin
30
100

temp_mixin2.d

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import std.stdio;

mixin template Value(){
  int value;
}

mixin Value Mona;
mixin Value Giko;

void main(){

  Mona.value = 10;
  Giko.value = 10;
  
  writeln("Mona.value = ", Mona.value);
  writeln("Giko.value = ", Giko.value);
}

temp_mixin2.d の実行結果は:

[cactus:~/code_d/d_tuts]% ./temp_mixin2
Mona.value = 10
Giko.value = 10

Previous topic

pure 関数

Next topic

文字列ミックスイン