> Also, interesting thing is that job.process() compiles but process(job) does 
> not.

You can solve this by adding "mixin process;" inside the generic proc, see 
<https://nim-lang.org/docs/manual.html#generics-symbol-lookup-in-generics> \- 
you have to keep it an "open" symbol.

Indeed, interesting and very confusing -- even though run() is only 
instantiated at the end, it doesn't have access to process because it is a 
"closed" symbol.

I added a 
    
    
     process[J](job: J): string = ""
    
    Run

in the beginning, which _does_ compile and run, but uses the generic 
process(job: SomeJob)

Running 
    
    
    # Generic function that expect that there should be
    # declared `id` field and `process` function for J type
    proc process[J](job: J): string = "generic value"
    
    proc run*[J](job: J): void =
      
      let id: string = job.id       # Expecting id field
      let v: string = process(job) # Expecting process proc
      echo "id=", id, " v", "=", v
    
    type SomeJob = object
      id: string
    
    proc process(job: SomeJob): string = "some value"
    
    run(SomeJob(id: "1"))
    
    echo process(SomeJob(id: "1"))
    
    
    Run

Produces this output:
    
    
    id=1 v=generic value
    some value
    
    
    Run

I would have been happy with a forward generic proc, but these are not accepted 
by the compiler (there is no mention in 
<https://nim-lang.org/docs/tut1.html#procedures-forward-declarations> whether 
it should or shouldn't)

So in this case, we have two "proc process(J:SomeJob):string" in the same 
file/scope, and both get included and executed, and the question about which 
one gets executed depends on the order of the definition between the generics 
and the instantiation. Am I misunderstanding something? I would expect at least 
a warning, if not an error - and also the ability to forward declare a generic 
proc, perhaps - though an error/warning in this case would be much more helpful.

Experiment in <https://play.nim-lang.org/#ix=2xiY>

Reply via email to