# Method of struct

**URL:** <https://discourse.julialang.org/t/method-of-struct/89458>\
**Category:** General Usage\
**Tags:** proposal\
**Created:** [October 28, 2022, 5:34pm UTC](https://discourse.julialang.org/t/method-of-struct/89458 "2022-10-28T17:34:39Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![duan](https://avatars.discourse-cdn.com/v4/letter/d/a9adbd/32.png) [@duan](https://discourse.julialang.org/u/duan)\
**Post date:** [October 30, 2022, 3:44pm UTC](https://discourse.julialang.org/t/method-of-struct/89458/21 "2022-10-30T15:44:40Z")

</div>

> [@lmiq](#):
>
> What one misses from the OO style is the autocompletion, but not the clarity of the syntax itself.

Since Julia doesn’t allow any function declaration other than constructors inside `struct` right now, one can imagine a new syntax that allows the declaration of the functions that are suitable for the dot syntax. For example,

```julia
struct Foo
    x
    function f(foo::Foo, x) end
end

f(foo:Foo, x) = (foo.x = x)

```

In this way, Julia can treat `foo.f(x)` as `f(foo, x)`. And it also makes autocompleetion easier to implement in editors.

---

<div class="post-metadata">

**Author:** ![duan](https://avatars.discourse-cdn.com/v4/letter/d/a9adbd/32.png) [@duan](https://discourse.julialang.org/u/duan)\
**Post date:** [October 30, 2022, 3:48pm UTC](https://discourse.julialang.org/t/method-of-struct/89458/22 "2022-10-30T15:48:57Z")

</div>

> [@aplavin](#):
>
> The situation is julia is much more consistent than eg in python.

Agreed.

> [@aplavin](#):
>
> Just try using instruments that julia gives you for some time, and probably you won’t miss the dot notation anymore.

Maybe. But the dot syntax seems so natural in many cases that the same issue will be raised again and again as long as Julia attracts people. Why not make it a syntax sugar?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 30, 2022, 5:06pm UTC](https://discourse.julialang.org/t/method-of-struct/89458/23 "2022-10-30T17:06:27Z")

</div>

> [@duan](#):
>
> Why not make it a syntax sugar?

Hasn’t that been explained sufficiently?

---

<div class="post-metadata">

**Author:** ![duan](https://avatars.discourse-cdn.com/v4/letter/d/a9adbd/32.png) [@duan](https://discourse.julialang.org/u/duan)\
**Post date:** [October 30, 2022, 6:06pm UTC](https://discourse.julialang.org/t/method-of-struct/89458/24 "2022-10-30T18:06:17Z")

</div>

> [@DNF](#):
>
> Hasn’t that been explained sufficiently?

I guess it depends who you ask 🙂 There’re things that one can manage to live without but will be happier to live with. Again, I’m not talking about piping or chaining for which elegant alternatives have been shown to exist.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 30, 2022, 6:36pm UTC](https://discourse.julialang.org/t/method-of-struct/89458/25 "2022-10-30T18:36:49Z")

</div>

> [@duan](#):
>
> one can imagine a new syntax that allows the declaration of the functions that are suitable for the dot syntax.

You can do that like this:

```julia
mutable struct Foo
    x::Float64
end
function Base.getproperty(obj::Foo, sym::Symbol)
    if sym == :f
        # return (val) -> setfield!(obj, :x, val)
        return (val) -> (obj.x = val)
    else
        return getfield(obj, sym)
    end
end

```

Then you can do

```julia
julia> obj = Foo(23.1)
Foo(23.1)

julia> obj.f(-4.2);

julia> obj
Foo(-4.2)

```

Do this for as many functions as you like. But it is not likely to become something that happens automatically.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [October 30, 2022, 6:42pm UTC](https://discourse.julialang.org/t/method-of-struct/89458/26 "2022-10-30T18:42:07Z")

</div>

I see the hornet’s nest has been kicked again 😉

> [@dfdx](#):
>
> To support this (again, pretty specific) case, I wrote a simple macro [`@chainable`](https://github.com/dfdx/Spark.jl/blob/main/src/chainable.jl)

Thank you, I have added this to my running list of Julia macros which hope to address chaining/piping/threading/currying/partial evaluation/whatever it’s called. I have now collected links to eleven such packages.

> [@DNF](#):
>
> You can read this long, and still active, thread on the subject

I feel like [my proposal](https://discourse.julialang.org/t/allowing-the-object-method-args-syntax-as-an-alias-for-method-object-args/62051/206) (plus [addendum](https://discourse.julialang.org/t/allowing-the-object-method-args-syntax-as-an-alias-for-method-object-args/62051/217)) is now mature enough that I can split off into its own proposal thread.

---

<div class="post-metadata">

**Author:** ![duan](https://avatars.discourse-cdn.com/v4/letter/d/a9adbd/32.png) [@duan](https://discourse.julialang.org/u/duan)\
**Post date:** [October 30, 2022, 7:32pm UTC](https://discourse.julialang.org/t/method-of-struct/89458/27 "2022-10-30T19:32:03Z")

</div>

> [@DNF](#):
>
> You can do that like this:
> 
> ```julia
> mutable struct Foo
> x::Float64
> end
> function Base.getproperty(obj::Foo, sym::Symbol)
> if sym == :f
> # return (val) -> setfield!(obj, :x, val)
> return (val) -> (obj.x = val)
> else
> return getfield(obj, sym)
> end
> end
> 
> ```

It’s good except that it may not be (elegantly) generalized to more complicated cases and it adds a performance penalty.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [October 30, 2022, 7:53pm UTC](https://discourse.julialang.org/t/method-of-struct/89458/28 "2022-10-30T19:53:06Z")

</div>

due to constant propagation, there is no performance penalty.

it’s just a dog darn ugly solution.

[Previous page](https://discourse.julialang.org/t/method-of-struct/89458.md?page=1)
