Search code examples
nim-lang

Nim setter procedure not being called


I'm trying to create a setter proc on my object

type
  Animal = ref object of RootObj
    name: string
    age: int

proc `age=`(a: var Animal, value: int) {.inline.} =
  echo("setting age!")
  a.age = value

var a: Animal
new a
a.age = 10

Why is this not working?


Solution

  • The field simply has precedence. Typically you just name the field something else and implement a setter and a getter. But this would also be resolved by not exporting the field but exporting the setter from the module.