tgenericvariant.nim 470 B

123456789101112131415161718192021222324
  1. type
  2. TMaybe[T] = object
  3. case empty: bool
  4. of false: value: T
  5. else: nil
  6. proc Just*[T](val: T): TMaybe[T] =
  7. result.empty = false
  8. result.value = val
  9. proc Nothing[T](): TMaybe[T] =
  10. result.empty = true
  11. proc safeReadLine(): TMaybe[string] =
  12. var r = stdin.readLine()
  13. if r == "": return Nothing[string]()
  14. else: return Just(r)
  15. when isMainModule:
  16. var Test = Just("Test")
  17. echo(Test.value)
  18. var mSomething = safeReadLine()
  19. echo(mSomething.value)