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)
    

Reply via email to