https://github.com/yglukhov/variant
Variant type and type matching for Nim
https://github.com/yglukhov/variant
Last synced: 7 months ago
JSON representation
Variant type and type matching for Nim
- Host: GitHub
- URL: https://github.com/yglukhov/variant
- Owner: yglukhov
- License: mit
- Created: 2015-12-24T20:41:27.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2024-08-12T14:31:14.000Z (about 1 year ago)
- Last Synced: 2024-10-14T15:04:31.733Z (12 months ago)
- Language: Nim
- Size: 50.8 KB
- Stars: 61
- Watchers: 4
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# variant [](https://github.com/yglukhov/variant/actions?query=branch%3Amaster) [](https://nimble.directory/pkg/variant)
Variant type and type matching for Nim```nim
import variantvar v = newVariant(5)
assert v.ofType(int)
assert v.get(int) == 5v = newVariant(3.0)
assert v.ofType(float)
assert v.get(float) == 3.0v = newVariant(@[1, 2, 3])
assert v.ofType(seq[int])
assert v.get(seq[int])[1] == 2
```Matching:
```nim
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]
```