No description
Find a file
2024-08-12 16:28:46 +02:00
.github/workflows Github actions 2022-02-16 17:26:58 +02:00
.gitignore Initial commit 2015-12-24 22:41:27 +02:00
LICENSE Initial commit 2015-12-24 22:41:27 +02:00
README.md README.md - playing with new nimble tag 2023-12-07 20:54:48 +02:00
variant.nim add isEmpty 2024-08-10 09:36:46 +03:00
variant.nimble Bump version to 0.3.1 2024-08-12 16:28:46 +02:00

variant Build Status nimble

Variant type and type matching for Nim

import variant

var v = newVariant(5)
assert v.ofType(int)
assert v.get(int) == 5

v = newVariant(3.0)
assert v.ofType(float)
assert v.get(float) == 3.0

v = newVariant(@[1, 2, 3])
assert v.ofType(seq[int])
assert v.get(seq[int])[1] == 2

Matching:

var v = newVariant(@[1, 2, 3])
assert v.ofType(seq[int])
variantMatch case v as u
of int:
    echo "u is int: ", u
of seq[int]:
    echo "u is seq[int]: ", u
else:
    echo "dont know what v is"

Will output:

u is seq[int]: @[1, 2, 3]