Best practice for adding validations to setfield! function?

I run into this problem with mutable structs where I’d like to validate the input.

Is there a best practices way to do this?


current psuedocode:

import Base: setfield!

mutable struct Foo
  fizz::Number
  is_good::Bool
end

function validate!(cur_obj::Foo)
  cur_obj.is_good = ( cur_fizz < 1 )
end

function setfield!(cur_obj::Foo, cur_field::Symbol, cur_value::Any)
  cur_super = Base.setfield!(cur_obj, cur_field, cur_value)
  validate!(cur_obj)
  cur_super
end

That definition calls itself the first thing it does so will never terminate.

Use setproperty! instead.

Very cool. Definitely a reason to upgrade to v0.7