Using the power of Nim to defile its type safety
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2024-01-12 05:47:54 -05:00
src Initial commit 2024-01-12 05:37:56 -05:00
tests Initial commit 2024-01-12 05:37:56 -05:00
.gitignore Initial commit 2024-01-12 05:37:56 -05:00
dynval.nimble Initial commit 2024-01-12 05:37:56 -05:00
LICENSE Initial commit 2024-01-12 08:18:35 +00:00
README.md fix typo 2024-01-12 05:47:54 -05:00

dynval

Using the power of Nim to defile its type safety

What?

This is a library that provides a type called DynVal which can box all primitives, strings and references to objects inheriting from RootObj. It provides necessary converters and type coercion to provide dynamic typing in Nim.

Importing this module will destroy the type safety and performance of your program.

Examples

import dynval

var thing1: DynVal = 10

assert thing1 == "10"

thing1 = true

if thing1:
    assert true

inc thing1

var thing2: DynVal = thing1 * 10
thing2 &= 1 + false

assert thing2 == "200" + 1
import dynval

var times: DynVal = "10"

proc loopNTimes(n: int) =
    for i in 0 ..< n:
        echo "i: ", i

loopNTimes(times)

See the tests directory for more examples.

What's missing?

  • Dynamic objects (basically tables with normal field access syntax)
  • Support for putting procs inside DynVal
    • This would require using a macro to generate typeguards, and then exposing a dot call operator with a signature like proc (args: varargs[auto]), and then doing magic on that for runtime type checking.