ZenScript main repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

variants.zs 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. public variant Result<T, E> {
  2. Ok(T),
  3. Error(E),
  4. Other(T, E);
  5. /*public then<R>(fn as function(result as T) as Result<R, E>) as Result<R, E> {
  6. return match this {
  7. Ok(result) => fn(result),
  8. Error(error) => Error(error),
  9. Other(result, error) => fn(result)
  10. };
  11. }*/
  12. //public handle<X>(handler as function(error as E) as Result<T, X>) as Result<T, X> {
  13. // return match this {
  14. // Ok(result) => Ok(result),
  15. // Error(error) => handler(error)
  16. // };
  17. //}
  18. public expect() as T {
  19. return match this {
  20. Ok(result) => result,
  21. Error(error) => panic "expect() called on an error value",
  22. Other(result, error) => result
  23. };
  24. }
  25. public orElse(other as T) as T {
  26. return match this {
  27. Ok(result) => result,
  28. Error(error) => other,
  29. Other(result, error) => result
  30. };
  31. }
  32. public orElse(other as function(error as E) as T) as T {
  33. return match this {
  34. Ok(result) => result,
  35. Error(error) => other(error),
  36. Other(result, error) => result
  37. };
  38. }
  39. }
  40. function makeResult() as Result<string, string>
  41. => Ok("10");
  42. function makeErrResult() as Result<string, string>
  43. => Error("10");
  44. println(makeResult().orElse("Ten"));
  45. println(makeResult().expect());
  46. println(makeErrResult().orElse("Ten"));
  47. //CompileException [TYPE_ARGUMENTS_NOT_INFERRABLE] Could not infer generic type parameters [ParsedExpressionFunction.compile, line 75]
  48. //println(makeResult().then(tValue => Result<string, string>.Ok(tValue)).expect());
  49. //IllegalArgumentException: Cannot retrieve members of undetermined type [TypeMembers.<init>, line 71]
  50. //println(makeResult().then(a => Ok(a)).expect());
  51. //println(makeResult().then(a as string => Ok(a)).expect());
  52. //CompileException [UNEXPECTED_TOKEN] ) expected [LLparserTokenStream.required, line 97]
  53. //Wants to compile a call to function() instead of creating a lambda
  54. //println(makeResult().then((function (t as string) as Result<string, string>)(t => Ok(t))).expect());