Skip to content

codehz/nim-vtable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vtable for nim

Why choose vtable?

Nim's dynamic dispatch cannot work across dynamic link libraries.

Features

  1. Define vtable in few lines!
  2. Use it as normal ref object!
  3. Can survival across dynlib!
  4. Type safe!
  5. Support nim's generic!

DOCS

Working in progress.

Example usage:

trait MyInterface:
  method sayName*(self: ref MyInterface)
  method add*(self: ref MyInterface, lhs, rhs: int): int

type MyClass = object of RootObj
  name: string

proc newMyClass(name: string): ref MyClass =
  new result
  result[].name = name

impl MyClass, MyInterface:
  method sayName(self: ref MyClass) =
    echo "I'm ", self.name

  method add(self: ref MyClass, lhs, rhs: int): int =
    lhs + rhs

proc testmethod(x: ref MyInterface, a: int): int =
  x.sayName()
  x.add(a, 2)

check 3 == testmethod(newMyClass(1))

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages