Skip to content

Instantly share code, notes, and snippets.

@east
Created July 10, 2015 12:11
Show Gist options
  • Save east/a31985a23be125bb63f0 to your computer and use it in GitHub Desktop.
Save east/a31985a23be125bb63f0 to your computer and use it in GitHub Desktop.
interfaces in nim
# Interface
type InterfaceType = ref object of RootObj
baseVar: int # optional var
# Virtual method of `InterfaceType`
method doSomething(this: InterfaceType) =
discard # empty method
# Implementation
type ImplementType = ref object of InterfaceType
someVar: int
someVar2: int
# Implementation of virtual method `doSomething`
method doSomething(this: ImplementType) =
this.someVar = 5
this.baseVar = 5
echo "two numbers: ", this.someVar, " ", this.baseVar
# Test
var impl = ImplementType()
impl.doSomething
# result: two numbers: 5 5
# Convert to base type
var base = InterfaceType(impl)
base.doSomething
# result: two numbers: 5 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment