Object-oriented syntax in julia

Please also see my answer to a related discussion that came up somewhat recently.

Also see this discussion on inheritance vs. composition.

But to summarize briefly, any inheritance structure can be restructured in terms of composition, and in many cases should be, even in OOP languages. Inheritance often seems like a natural and intuitive way of structuring code but almost always creates problems in the long-run; in my experience, the most common anti-patterns are fragile base classes, subclass repetition, and god-types, all of which are typically rampant in inheritance-heavy code. Composition has its own pitfalls but is generally a much more scalable pattern and is also better for encapsulation.

As for the obj.func() syntax, no this will not happen. And I disagree that this is ever really a helpful way of writing code. There is simply no reason to arbitrarily group together functions and data like in OOP. It’s just not necessary and is rarely helpful (outside of maybe the convenience of tab-completion). Any time you have an “object” which you think of as doing something, you could always alternatively think of it as an independent action which is applied to that object. And this is, in fact, the better and more scalable way to think about it in most use cases, especially the scientific and mathematical ones for which Julia excels as a language.

8 Likes