I wanted to know if there is a way we can make instance variables inaccessible outside of the class. For example, the following snippet (which works and prints 30):
class Point
@x : Int32
@y : Int32
def initialize(@x, @y)
end
end
point = Point.new(10, 20)
puts point.@x + point.@y
What if I wanted @x and @y not be accessible from outside the class if I do not define any getter or property. I think I read in a discussion that instance variables are private by default, is that only in case of writes?
Thanks in advance. I just started learning Crystal two days ago so if I have missed the obvious then apologies in advance too.
There is no way to do that. Instance variables are actually public, but you should write code as if they were private. So here you should use two getters.
Even if we remove the syntax to access an instance variable from outside of a class, anyone can reopen your class and add getters for them. In Ruby this is almost the same, you have a method called instamcr_variable_get.
Thank you very much everyone, I titally agree and just wanted to verify. This code smell has a pretty strong scent since a .@ will give it away upon visual inspection. The 0.01% of the time I can see myself using it is when I’m debugging and even that would be temporary.
It’s awesome to have replies so fast. Y’all are awesome :)