The Wayback Machine - https://web.archive.org/web/20221023114928/https://github.com/Carpall/nobject
Skip to content

carpall/nobject

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 

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

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages