# Object-oriented syntax in julia

**URL:** <https://discourse.julialang.org/t/object-oriented-syntax-in-julia/90017>\
**Category:** General Usage\
**Tags:** discussion\
**Created:** [November 10, 2022, 1:57am UTC](https://discourse.julialang.org/t/object-oriented-syntax-in-julia/90017 "2022-11-10T01:57:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)\
**Post date:** [November 10, 2022, 1:57am UTC](https://discourse.julialang.org/t/object-oriented-syntax-in-julia/90017/1 "2022-11-10T01:57:28Z")

</div>

I learned and wrote some c++ code recently. Although I’m convinced that the whole system including like inheritance and modifiers (private vs public) is good, I do think the syntax `foo.func(...)` has some benefits.

I know julia does not support this syntax and the whole community thinks that this is equivalent to `func(foo, ...)`. However, the different ordering does emphasize different things: The julia syntax emphasizes the function, while the OO syntax emphasizes the “subject” `foo`. In some situations, I think the OO syntax is more readable, reasonable, and discoverable.

Moreover, the OO system is more than just the syntax. The encapsulation makes the code more organized. Although julia has `module`, associating a function with a particular structure could be useful in some circumstances.

Having said that, I found [this post on Reddit](https://www.reddit.com/r/Julia/comments/nnu6if/comment/gzxcx8s/?utm_source=share&utm_medium=web2x&context=3), where Eigenspace described a way to achieve what I described above in julia. Hopefully this can be useful for people who also want some lightweight OO support in julia.

---

<div class="post-metadata">

**Author:** ![xgdgsc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xgdgsc/32/608_2.png) [@xgdgsc](https://discourse.julialang.org/u/xgdgsc)\
**Post date:** [November 10, 2022, 4:54am UTC](https://discourse.julialang.org/t/object-oriented-syntax-in-julia/90017/2 "2022-11-10T04:54:15Z")

</div>

> **[GitHub - Suzhou-Tongyuan/ObjectOriented.jl: Conventional object-oriented...](https://github.com/Suzhou-Tongyuan/ObjectOriented.jl)**
>
> Conventional object-oriented programming in Julia without breaking Julia's core design ideas - GitHub - Suzhou-Tongyuan/ObjectOriented.jl: Conventional object-oriented programming in Julia with...

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [November 10, 2022, 6:59am UTC](https://discourse.julialang.org/t/object-oriented-syntax-in-julia/90017/3 "2022-11-10T06:59:36Z")

</div>

Hi,

One can take a sentence in one human language, and by and large create at sentence in another language that expresses the same idea.

Not so with computer programming languages. To some extent, they impose their way of thinking about the code. You cannot take a C++ code and “translate” it in Julia - you have to rewrite the code, and the reorganisation can be quite deep.

Hence, learning a new programming language goes way beyond getting familiar with a new vocabulary. It’s about learning a new way to organise code, and appreciate it’s power. And that takes time. The first step, in Zen terms: now clean your bowl - do not let your knowledge get in the way.

😀

---

<div class="post-metadata">

**Author:** ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)\
**Post date:** [November 10, 2022, 10:59am UTC](https://discourse.julialang.org/t/object-oriented-syntax-in-julia/90017/4 "2022-11-10T10:59:48Z")

</div>

Yes, different programming languages have different styles. Nonetheless, different styles do not necessarily exclude each other. For example, OOP is part of the design of Python and very popular in the community, but there is an official library for functional programming, which allows people to write code in a different style when it is more suitable.

Personally speaking, I believe the function-oriented style is more natural for scientific computing in most situations, and this is one reason why I like julia. However, there are situations where other styles are more natural. For example, for agent-based modeling I think OOP is more natural.

Just like python doesn’t have FP in its design, julia needs not to support OOP directly, but a method to mimic the OO syntax is good, and the Reddit post gives a way to do that, without sacrificing the performance (at lease in that simple example).

The full OOP has much more than the `foo.func()` syntax, for example, inheritance. Some people said julia has already supported OOP and multiple dispatch is more general than OOP. This claim oversimplifies OOP, which is a quite complicated beast, more than just a function call syntax or polymorphism. Therefore, writing `foo.func()` is far away from OOP. (This is actually called UFCS, and has nothing to do with OOP.)

In short, I’m not saying doing a full OOP in julia, but a minimal OO syntax could help the code with readability in some situations, and readability is an important principle in julia’s design. We should not feel afraid of this coding style.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 10, 2022, 11:39am UTC](https://discourse.julialang.org/t/object-oriented-syntax-in-julia/90017/5 "2022-11-10T11:39:12Z")

</div>

In short - no, this is not and will not be an official thing. There are numerous threads about this topic already. Please familiarize yourself with the existing discussions before starting a new one.

[https://discourse.julialang.org/search?q=OOP](https://discourse.julialang.org/search?q=OOP)

[https://discourse.julialang.org/search?q=object%20oriented](https://discourse.julialang.org/search?q=object%20oriented)

> [@PSA: Julia is not at that stage of development anymore](https://discourse.julialang.org/t/psa-julia-is-not-at-that-stage-of-development-anymore/44872):
>
> You heard about this brand new exciting programming language called Julia and have some suggestions for how it should work. Wonderful! We’re happy to have you here and help you learn the language and listen to your ideas! However, please be aware that “new” is a relative term. Yes, Julia is newer than, say, C, which was first created in 1972 and standardized in 1989. It is not, however, new in the sense that it is still being freely designed and changed in arbitrary ways. Julia development start…

There’s also this thread, which has VERY extensive discussion about this syntax and why it won’t be a thing:

> [@Allowing the object.method(args...) syntax as an alias for method(object, args ...)](https://discourse.julialang.org/t/allowing-the-object-method-args-syntax-as-an-alias-for-method-object-args/62051):
>
> I started exploring Julia very recently and I like it. I’ve been chatting about it with colleagues. Many people coming from OOP languages (Python…) like the syntax object.method(a,b,c…). Since you can get a preview of the available methods acting on object with tab press. Would it be possible to have that in Julia (I’m assuming it’s not already there) to improve usability? Let say I define a IIR filter struct mutable struct IIRfilter state; alpha; IIRfilter(state,alpha) = 0\<=alpha\<=1 ? …

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [November 10, 2022, 11:43am UTC](https://discourse.julialang.org/t/object-oriented-syntax-in-julia/90017/6 "2022-11-10T11:43:11Z")

</div>

I reckon it would be relatively\* easy to create a macro to transform `obj.foo(...)` into `foo(obj,...)` (and maybe something similar on the method definition side) and thus emphasize an object oriented thinking.

But inheritance is harder. The best I can see in Julia is to give a child class one (or more…) property `parent` which contains an object of the parent class. And then, one must introduce a parallel system of abstract classes `Aparent` `Achild`, with `Parent<:Aparent`, `Child<:Achild`, and dispatch on these. It’s not going to be pretty, though, unless a smart metaprogrammer hides the kludginess behind some macros.

Then it’s a matter of taste…

(\* relatively is the key word when it comes to metaprogramming! 😁)

---

<div class="post-metadata">

**Author:** ![bgroenks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bgroenks/32/21784_2.png) [@bgroenks](https://discourse.julialang.org/u/bgroenks)\
**Post date:** [November 10, 2022, 1:36pm UTC](https://discourse.julialang.org/t/object-oriented-syntax-in-julia/90017/7 "2022-11-10T13:36:34Z")

</div>

Please also see [my answer](https://discourse.julialang.org/t/why-there-is-no-oop-object-oriented-programming-in-julia/86723/58) to a related discussion that came up somewhat recently.

Also see this [discussion on inheritance vs. composition](https://discourse.julialang.org/t/composition-and-inheritance-the-julian-way/11231/14).

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.

---

<div class="post-metadata">

**Author:** ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)\
**Post date:** [November 10, 2022, 6:22pm UTC](https://discourse.julialang.org/t/object-oriented-syntax-in-julia/90017/8 "2022-11-10T18:22:48Z")

</div>

Well, I’m not saying this should be supported by the language itself. Maybe a macro can help. Also I agree that composition is better than inheritance.

My point is the syntax. Of course we can turn everything into the `func(obj, ...)` way, but in the situation where the action is tightly related to the object, it is possible that the other way makes more sense. These situations are indeed relatively rare in science and math, but they do exist and I could not see why writing code in this style is harmful in those circumstances.

---

<div class="post-metadata">

**Author:** ![bgroenks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bgroenks/32/21784_2.png) [@bgroenks](https://discourse.julialang.org/u/bgroenks)\
**Post date:** [November 11, 2022, 5:26pm UTC](https://discourse.julialang.org/t/object-oriented-syntax-in-julia/90017/9 "2022-11-11T17:26:19Z")

</div>

It’s harmful because it creates inconsistency in the idiomatic way to write code in the language. As I think @Raf pointed out in a different thread, it also creates ambiguity since `obj.func()` already has a syntactic meaning in Julia. All types can be made callable, so it means that you are invoking the field `func` on `obj` (which you can actually use to implement a kind of object-oriented pattern, although it’s generally discouraged). `obj::func()` and `obj->func()` are also bad because they conflict with existing syntax. The language would need to introduce a new syntax to support this, and it would then probably defeat the purpose since it would no longer seem familiar to people coming from OOP languages.

---

<div class="post-metadata">

**Author:** ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)\
**Post date:** [November 11, 2022, 6:00pm UTC](https://discourse.julialang.org/t/object-oriented-syntax-in-julia/90017/10 "2022-11-11T18:00:43Z")

</div>

By

> it also creates ambiguity since `obj.func()` already has a syntactic meaning in Julia

do you mean the `func()` is in a module?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 11, 2022, 6:25pm UTC](https://discourse.julialang.org/t/object-oriented-syntax-in-julia/90017/11 "2022-11-11T18:25:51Z")

</div>

no. If I make

```julia
struct A
   func
end
obj=A(print)
obj.func("hi")

```

it will access the `func` field of `obj` and call it.

---

<div class="post-metadata">

**Author:** ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)\
**Post date:** [November 11, 2022, 6:43pm UTC](https://discourse.julialang.org/t/object-oriented-syntax-in-julia/90017/12 "2022-11-11T18:43:05Z")

</div>

So you mean the same syntax can mean either a callable type, or a function defined in `getProperty`. Ok this is a good reason to not have this syntax.
