summaryrefslogtreecommitdiffhomepage
path: root/CompilerDriver/cc2/regression-tests/pure2-union.cpp2
blob: 196f41c0143846520a708273ff0f230bb76882a0 (plain)
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

name_or_number: @union type = {
    name: std::string;
    num : i32;
}

name_or_other: @union <T:type> type
= {
    name  : std::string;
    other : T;

    to_string: (this) -> std::string = {
        if is_name()       { return name(); }
        else if is_other() { return other() as std::string; }
        else               { return "invalid value"; }
    }
}

print_name: (non: name_or_number) = {
    if non.is_name() {
        std::cout << non.name() << "\n";
    }
    else {
        std::cout << "(not a name)\n";
    }
}

main: () = {
    x: name_or_number = ();
    std::cout << "sizeof(x) is (sizeof(x))$\n";

    x.print_name();

    x.set_name( "xyzzy", 3 as u8 );

    x.print_name();

    {
        val: name_or_other<int> = ();
        val.set_other(42);
        std::cout << val.to_string();
    }
}