3 ms·
This is why Kotlin has the best of both worlds. You can access a variable through A.b and it will automatically call the getter if there's one
by The_rationalist 6y ago
This is why Kotlin has the best of both worlds. You can access a variable through A.b and it will automatically call the getter if there's one
- mgkimsal 6y agogroovy did/does this too. any property automatically has a getter/setter in the compiled code if an explicit getter or setter doesn't exist.
- kabes 6y agoJavascript can do the same, but I think it's an anti pattern, since now the caller doesn't know what side effects, or performance characteristics you get by accessing the property with an implicit getter/setter.
- dragonwriter 6y ago> now the caller doesn't know what side effects, or performance characteristics you get by accessing the property with an implicit getter/setter. Beyond method call overhead (which may or may not be a given depending on how things are optimized in some languages), the caller doesn't know that automatically with explicit getters/setters, either; you'd have to read the source to know that just as much if they are explicit as implicit (or rely on documentation, which hopefully is complete and current).
- augustk 6y agoIn the programming language Eiffel this is called the uniform access principle.
- edflsafoiewq 6y agoThe uniform access principle is that features are "available through a uniform notation, which does not betray whether they are implemented through storage or through computation"; it doesn't imply anything about what the uniform notation is. Java-style getters/setters also achieve the uniform access principle; everything is accessed only via get_X()/set_X() and never a raw field .X that would betray it was implemented through storage.
- augustk 6y ago"Although it's a fundamental feature of object-oriented programming, remarkably few OO languages really follow it. (Eiffel is naturally an exception.) Most languages simulate it by convention, hence the habit of using accessors such as getAge in Java and C++ programs." https://martinfowler.com/bliki/UniformAccessPrinciple.html https://martinfowler.com/bliki/UniformAccessPrinciple.html