| 1 | struct Calc[S] { |
| 2 | mut: |
| 3 | typ S |
| 4 | } |
| 5 | |
| 6 | struct TypeA {} |
| 7 | |
| 8 | struct TypeB {} |
| 9 | |
| 10 | fn (mut c Calc[S]) next[T](input T) f64 { |
| 11 | $if S is TypeA || S is TypeB { |
| 12 | return c.typ.next(input) |
| 13 | } $else { |
| 14 | return 99.0 |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | fn (mut t TypeA) next[T](_input T) f64 { |
| 19 | return 10 |
| 20 | } |
| 21 | |
| 22 | fn (mut t TypeB) next[T](_input T) f64 { |
| 23 | return 11 |
| 24 | } |
| 25 | |
| 26 | @[markused] |
| 27 | fn new[S]() Calc[S] { |
| 28 | $if S is TypeA { |
| 29 | return Calc[TypeA]{ |
| 30 | typ: TypeA{} |
| 31 | } |
| 32 | } $else $if S is TypeB { |
| 33 | return Calc[TypeB]{ |
| 34 | typ: TypeB{} |
| 35 | } |
| 36 | } $else { |
| 37 | panic('unknown type ${S.name}') |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | fn main() { |
| 42 | { |
| 43 | mut c := Calc[TypeA]{ |
| 44 | typ: TypeA{} |
| 45 | } |
| 46 | assert c.next(100) == 10.0 |
| 47 | } |
| 48 | { |
| 49 | mut c := Calc[TypeB]{ |
| 50 | typ: TypeB{} |
| 51 | } |
| 52 | assert c.next(100) == 11.0 |
| 53 | } |
| 54 | println('OK!!') |
| 55 | } |
| 56 | |