Another solution:
import future
type
MyInterface = ref object of RootRef
foo: proc(this: MyInterface): int
bar: proc(this: MyInterface): int
proc foobar(this: MyInterface): int =
return this.foo(this) + this.bar(this)
type
MyImplementationA = ref object of MyInterface
MyImplementationB = ref object of MyInterface
m_foo, m_bar: int
proc newMyImplementationA(): MyImplementationA =
result = new(MyImplementationA)
result.foo = proc (this: MyInterface): int = 17
result.bar = proc (this: MyInterface): int = 4
proc newMyImplementationB(m_foo, m_bar: int): MyImplementationB =
result = new(MyImplementationB)
result.m_foo = m_foo
result.m_bar = m_bar
result.foo = proc (this: MyInterface): int = MyImplementationB(this).m_foo
result.bar = proc (this: MyInterface): int = MyImplementationB(this).m_bar
var a = newMyImplementationA()
var b = newMyImplementationB(32, 8)
echo foobar(a)
echo foobar(b)
- Re: Go-lang like interface Krux02
- Re: Go-lang like interface bpr
- Re: Go-lang like interface Krux02
- Re: Go-lang like interface jangko
- Re: Go-lang like interface Krux02
- Re: Go-lang like interface Dippo
- Re: Go-lang like interface _tulayang
- Re: Go-lang like interface Krux02
- Re: Go-lang like interface OderWat
- Re: Go-lang like interface _tulayang
- Re: Go-lang like interface _tulayang
- Re: Go-lang like interface Krux02
- Re: Go-lang like interface _tulayang
- Re: Go-lang like interface Krux02
- Re: Go-lang like interface OderWat
- Re: Go-lang like interface _tulayang
- Re: Go-lang like interface Krux02
- Re: Go-lang like interface jester
- Re: Go-lang like interface Krux02
- Re: Go-lang like interface bpr
- Re: Go-lang like interface Krux02
