Unfortunately, that is not that simple...

Like I said in the introduction, the templates are generated by another 
template. So the real code of my project looks like:
    
    
    template generate(op: untyped, Typ1, Typ2, Tout: untyped) =
      template `op`(a: `Typ1`, b: typed): `Tout` =
        when typeof(b) isnot `Typ2`:
          {.error: "Second param " & astToStr(b) & " of == with Real is not a 
float, but is " & $typeof(b).}
        a.val == b
      
      template `op`(a: typed, b: `Typ1`): `Tout` =
        when typeof(a) isnot `Typ2`:
          {.error: "First param " & astToStr(b) & " of == with Real is not a 
float, but is " & $typeof(b).}
        a == b.val
    
    # Generate operators for all types
    generate(`==`, Real, float, bool)
    etc.
    ...
    
    
    Run

Now the when code executes when **I** compile my module while generating the 
templates for all operators x types, and it aborts compilation (with a cryptic 
error message like Type expected: got bool or Error: type expected, but got 
symbol 'bool' of kind 'Template' if I change is not for isnot in the module 
code!)

Instead, it should happen when **the user** compiles his code with invalid 
parameters.

At least, that's how I interpret what's happening. The bug occurs only when I 
have the 2 lines in the generated templates:
    
    
    when typeof(b) isnot `Typ2`:
          {.error: "Second param " & astToStr(b) & " of == with Real is not a 
float, but is " &
    
    
    Run

But there must be another real reason because when I try with all the other 
types, I don't get the problem; only with the bool parameter. I spent too much 
time today on that bug to have a clear vision. At some time during the day, 
when I tried different forms of the code, I got [the same 
error](https://forum.nim-lang.org/t/3497), but now with Nim 1.2.0 the error has 
changed...

Perhaps it's time to convert this template to a macro and learn how to inject 
the when ... lines?

Reply via email to