@Krux02 Thanks for the hint! I've already gone with the solution advised by 
@Araq though:
    
    
    import macros
    
    type
      ValidationError* = object of Exception
    
    template validateLength(limit: Natural) {.dirty.} =
      if len(value) > limit:
        raise newException(ValidationError, "The value is too long.")
    
    macro maxLength*(limit: Natural, setterProc: untyped): untyped =
      setterProc.body.insert(0, getAst(validateLength(limit)))
      result = setterProc
    

I think it's more readable than using `setterProc.params[2][0]`, because these 
indices don't really tell anything to the code reader. So it may be a good 
thing I couldn't make it work the `quote` way :)

Still, I wonder if using dirty templates in conjunction with `getAst` is the 
easiest way to emulate decorator behavior. I have a sense there must be a 
better way (not that this way is bad).

Reply via email to