Created
July 10, 2015 12:11
-
-
Save east/a31985a23be125bb63f0 to your computer and use it in GitHub Desktop.
interfaces in nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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