I'm trying to understand how generics are instantiated; I figured they would 
let me separate the generic declaration with a specified implementation in a 
different module, but it seems they don't work that way in practice. I'm trying 
to build an MCTS library that doesn't rely on specific details of the data 
structures and algorithms being used, for example:

Consider `a.nim`, which has a generic `dostuff` method that relies on an 
algorithm specified elsewhere:
    
    
    # a.nim
    proc impl*[T](x: T): string =
      quit "bug: override me"
    
    proc dostuff*[T](x: T) =
      echo "Calling: ", impl(x)
    
    
    Run

The actual implementation of `impl` is in `main.nim`: 
    
    
    # main.nim
    import a
    
    type Foo = object
    
    proc impl*(x: Foo): string =
      return "object: Foo()"
    
    dostuff(Foo())
    
    
    Run

This doesn't work, I expected "Calling: object: Foo()" but the override 
resolution instead uses the generic definition.

Functions like `$` work this way seamlessly. I can have one module define a 
`proc `$`*(x: Foo)` and another module that doesn't require it can call `$` on 
the object, so I don't understand why this works

Reply via email to