As.k 856 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. invoke {
  2. // Type 1
  3. struct Vector {
  4. x: Number,
  5. y: Number
  6. }
  7. config {
  8. operator as (v, T) {
  9. assert T ~~ Pair
  10. return Pair(v.x, v.y)
  11. }
  12. }
  13. // Type2
  14. class Pair {
  15. init (a: Any, b: Any) {
  16. do nothing
  17. }
  18. get_a () -> Object {
  19. return a
  20. }
  21. get_b () -> Object {
  22. return b
  23. }
  24. operator as (p, T) {
  25. assert T ~~ Vector
  26. assert p->get_a() is Number
  27. assert p->get_b() is Number
  28. return Vector { x: p->get_a(), y: p->get_b() }
  29. }
  30. }
  31. // Cast between Type1 and Type2
  32. let v = Vector { x: 1, y: 3 }
  33. let vp = v as Pair
  34. assert vp -> get_a == 1
  35. assert vp -> get_b == 3
  36. let pv = vp as Vector
  37. assert pv.x == 1
  38. assert pv.y == 3
  39. }