Does Crystal have attribute accessor methods like in Ruby?

187 Views Asked by At

Does the Crystal programming language have an equivalent to Ruby's attribute accessor methods? More specifically, does Crystal have equivalents to the following?

  • attr_accessor
  • attr_reader
  • attr_writer

?

1

There are 1 best solutions below

3
Andre Wildberg On BEST ANSWER

Yes, they're defined as macro.

Basically:

ruby crystal
attr_accessor property
attr_reader getter
attr_writer setter

Example

class Person
  property name
end

is equal to

class Person
  def name=(@name)
  end

  def name
    @name
  end
end

For more details see the Reference