The Crystal Programming Language Forum

Getter, setter and property

Hi there,

I just try to figure out were the differenz is between the ‘property’ macro …

#property
class Person
    def name=(@name)
    end
  
    def name
      @name
    end
end

… and the ‘getter’ and ‘setter’ macro

# getter
class Person
    def name
        @name
    end
end  
  
# setter
class Person
    def name=(@name)
    end
end

It seems, that ‘property’ combines ‘getter’ & ‘setter’?

Is there any reason to use ‘getter’ and ‘setter’ instead of ‘property’? (In case I want be able to set and get ‘name’.)

Thanks

Bob

No.
As far as I know they are for when you want to get or to set the value, but not both.

1 Like

That’s correct.

As @chillfox already said, no not really. The only use case i can think of is if you wanted to add specific documentation to one or the other. However at this point you’d likely not be using the macro as there’d be custom logic in the method that wouldnt be supported with the macro.

2 Likes