| 1 | interface Any {} |
| 2 | |
| 3 | struct Concrete { |
| 4 | a int |
| 5 | } |
| 6 | |
| 7 | struct Container { |
| 8 | concrete Any |
| 9 | } |
| 10 | |
| 11 | fn (container &Container) get_first_struct[T]() !&T { |
| 12 | concrete := container.concrete |
| 13 | if concrete is T { |
| 14 | println(concrete.a) |
| 15 | return concrete |
| 16 | } |
| 17 | return error("can't cast") |
| 18 | } |
| 19 | |
| 20 | fn test_generic_empty_interface_to_struct() { |
| 21 | concrete := Concrete{12345} |
| 22 | container := Container{concrete} |
| 23 | cast_concrete := container.get_first_struct[Concrete]() or { &Concrete{} } |
| 24 | assert 12345 == cast_concrete.a |
| 25 | } |
| 26 | |