mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-01 09:31:18 +00:00
Added a generics test case.
This commit is contained in:
23
tests/accept/compile/tgenericvariant.nim
Normal file
23
tests/accept/compile/tgenericvariant.nim
Normal file
@@ -0,0 +1,23 @@
|
||||
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 = stdin.readLine()
|
||||
if r == "": return Nothing[string]()
|
||||
else: return Just(r)
|
||||
|
||||
when isMainModule:
|
||||
var Test = Just("Test")
|
||||
echo(Test.value)
|
||||
var mSomething = safeReadLine()
|
||||
echo(mSomething.value)
|
||||
Reference in New Issue
Block a user