case_ref.km 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. type Value enum {
  2. type Num Float;
  3. type Text String;
  4. };
  5. function Num: &(Float) => Value
  6. &(x) => { |Value| { |Num| x } };
  7. function Text: &(String) => Value
  8. &(str) => { |Value| { |Text| str } };
  9. type ValuePair {
  10. first: Value,
  11. second: Value
  12. };
  13. function String:
  14. &(Value) => String
  15. &(val) =>
  16. switch val:
  17. case Num x:
  18. { String x },
  19. case Text t:
  20. { quote t },
  21. end;
  22. function describe:
  23. &(Result[Value,Value]) => String
  24. &(result) =>
  25. switch result:
  26. case Success val:
  27. val.{String}.{"(ok ?)"},
  28. case Failure val:
  29. val.{String}.{"(failed ?)"},
  30. end;
  31. do
  32. // TODO: test for the whole return value of ref call (pair[0], pair[1])
  33. let a: Result[Value,Value] := { Success { Num 1.0 } },
  34. let b: Result[Value,Value] := { Failure { Text '2' } },
  35. let c := { |ValuePair| { first: { Num 3.0 }, second: { Text '3' } } },
  36. \ p1 := assert-some a.(Success).{->},
  37. \ p2 := assert-some b.(Failure).(Text).{->}.{ map |Value| },
  38. let q1 := (a.(Success) <- { Num 77.0 }),
  39. let q2 := (a.(Success) <- { Text '88' }),
  40. let q3 := (b.(Failure).(Text) <- '99'),
  41. let r := (b.(Success).(Text) <- 'bad'),
  42. \ s1 := assert-some c.(first).(Num).{->},
  43. let { s2: second } := (c.(second).(Text) <- '9'),
  44. let str := [
  45. p1.{String},
  46. p2.{String},
  47. q1.{self::describe},
  48. q2.{self::describe},
  49. q3.{self::describe},
  50. r.{self::describe},
  51. s1.{String},
  52. s2.{String}
  53. ].{join \n},
  54. { os::println str }
  55. . { crash-on-error };