tgenericvariant.nim 594 B

12345678910111213141516171819202122232425262728293031323334353637
  1. discard """
  2. output: '''
  3. Test
  4. abcxyz123
  5. '''
  6. """
  7. proc fakeReadLine(): string =
  8. "abcxyz123"
  9. type
  10. TMaybe[T] = object
  11. case empty: bool
  12. of false: value: T
  13. else: nil
  14. proc Just*[T](val: T): TMaybe[T] =
  15. result.empty = false
  16. result.value = val
  17. proc Nothing[T](): TMaybe[T] =
  18. result.empty = true
  19. proc safeReadLine(): TMaybe[string] =
  20. var r = fakeReadLine()
  21. if r == "": return Nothing[string]()
  22. else: return Just(r)
  23. proc main() =
  24. var Test = Just("Test")
  25. echo(Test.value)
  26. var mSomething = safeReadLine()
  27. echo(mSomething.value)
  28. mSomething = safeReadLine()
  29. main()