summary refs log tree commit diff stats
path: root/tests/generics/tgenericvariant.nim
blob: 5ba3a2e7ca8a69c23d6f3e0d26f6255cdd1aad1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
discard """
output: '''
Test
abcxyz123
'''
"""

proc fakeReadLine(): string =
  "abcxyz123"

type
  TMaybe[T] = object
    case empty: bool
    of false: value: T
    else: nil

proc Just*[T](val: T): TMaybe[T] =
  result.empty = false
  result.value = val

proc Nothing[T](): TMaybe[T] =
  result.empty = true

proc safeReadLine(): TMaybe[string] =
  var r = fakeReadLine()
  if r == "": return Nothing[string]()
  else: return Just(r)

proc main() =
  var Test = Just("Test")
  echo(Test.value)
  var mSomething = safeReadLine()
  echo(mSomething.value)
  mSomething = safeReadLine()

main()