Constructor.addConstructorSequence

Add a constructor function from sequence.

class Constructor
void
addConstructorSequence
@safe nothrow
(
T
)
(
const string tag
,
T function(
ref Node
)
ctor
)

Examples

1 import std.string;
2 
3 import dyaml.all;
4 
5 struct MyStruct
6 {
7    int x, y, z;
8 
9    //Any D:YAML type must have a custom opCmp operator.
10    //This is used for ordering in mappings.
11    const int opCmp(ref const MyStruct s)
12    {
13        if(x != s.x){return x - s.x;}
14        if(y != s.y){return y - s.y;}
15        if(z != s.z){return z - s.z;}
16        return 0;
17    }
18 }
19 
20 MyStruct constructMyStructSequence(ref Node node)
21 {
22    //node is guaranteed to be sequence.
23    //!mystruct [x, y, z]
24    return MyStruct(node[0].as!int, node[1].as!int, node[2].as!int);
25 }
26 
27 void main()
28 {
29    auto loader = Loader("file.yaml");
30    auto constructor = new Constructor;
31    constructor.addConstructorSequence("!mystruct", &constructMyStructSequence);
32    loader.constructor = constructor;
33    Node node = loader.load();
34 }

See Also

addConstructorScalar

Meta