nobject

A dynamic object for dynamic typing in nim, allowing recursive types. Inspired by .net object

Pure Nim score 18/100 · last commit 2021-01-26 · 17 stars · tests present · no docs generated

Summary

Latest Version Unknown
License Unknown
CI Status Failing
Stars 17
Forks 2
Open Issues 2
Last Commit 2021-01-26
Downloads 0
Last Indexed 2026-07-21 05:25

Installation

nimble install nobject
choosenim install nobject
git clone https://github.com/carpall/nobject

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
nobject - - - - - - -

Source

Repository https://github.com/carpall/nobject
Homepage https://github.com/carpall/nobject
Registry Source github

README

Here a simple object for allowing dynamic typing in nim

Object is built on a variant object

It supports the following types: int8, int32, int64, float64, string, char, array of Object, custom instances (like structs)

This is a simple library to use and manage it by code. You can use it in structs, vars, etc..

Example

# Stack allocation

var s: Object = "Hello"      # new object of type string
echo s                       # Hello, calling $
echo s.getType()             # ocString, where oc means ObjectCode
let x: Object = s of string  # true
s = x                        # changing type at runtime
if x:
  echo "Implicit conversion to bool"

Another example

type
  Person = object
    name: string
    age: uint8

var tmp = Person()
var me: Object = newObject(tmp)

var other = me
other.name = "?"

echo "Recovering name: ", (me as Person).name
assert me != other

Using it in types

type
  Node = object
    kind: uint8
    value: Object                 # Allowing Node recursion, normally illegal
  DynamicType = object
    node: Object

var dyn = DynamicType(node: null) # null = newObject(ocNull), declared in the module
var node = Node(value: null)
dyn.node = Node(value: newObject(node)) # type recursion