# Do julia structs have member functions (and this-\>)

**URL:** https://discourse.julialang.org/t/do-julia-structs-have-member-functions-and-this/8839
**Category:** New to Julia
**Created:** [February 5, 2018, 4:57pm UTC](https://discourse.julialang.org/t/do-julia-structs-have-member-functions-and-this/8839 "2018-02-05T16:57:03Z")
**Posts on this page:** 9
**Page:** 2

<div class="post-metadata">

### Author: ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)
#### Post date: [August 12, 2019, 8:28pm UTC](https://discourse.julialang.org/t/do-julia-structs-have-member-functions-and-this/8839/21 "2019-08-12T20:28:03Z")

</div>

thanks !

---

<div class="post-metadata">

### Author: ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)
#### Post date: [October 25, 2021, 6:24pm UTC](https://discourse.julialang.org/t/do-julia-structs-have-member-functions-and-this/8839/22 "2021-10-25T18:24:35Z")

</div>

> [@foobar\_lv2](#):
>
> ```julia
> function Foo()
> x::Int = -1
> setx(x_) = (x = x_) 
> getx() = x
> () -> (getx;setx)
> end
> 
> ```

Old post, but could not find a more recent example.  
In oo-lingo, closures getx and setx are made to “public methods”.

> () → (getx;setx)

I understand something like “tuple”, “map”, “tuple of functions”, but not the syntax.  
Could someone please translate the working of this line into plain English?  
Which part of the doc to consult for such an expression?  
Thx

---

<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: [October 25, 2021, 9:33pm UTC](https://discourse.julialang.org/t/do-julia-structs-have-member-functions-and-this/8839/23 "2021-10-25T21:33:37Z")

</div>

This defines an anonymous function with zero arguments. Calling it returns a named tuple containing the functions `getx` and `setx`.

I.e. you’d use it like this:

```julia
julia> f = Foo()

julia> f().getx()
-1

julia> f().setx(1)
1

julia> f().getx()
1

```

[https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions](https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions)

---

<div class="post-metadata">

### Author: ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)
#### Post date: [October 26, 2021, 10:52am UTC](https://discourse.julialang.org/t/do-julia-structs-have-member-functions-and-this/8839/24 "2021-10-26T10:52:07Z")

</div>

Thanks @Sukera.

> This defines an anonymous function with zero arguments.

Right

> Calling it returns a named tuple containing the functions `getx` and `setx` .

Can’t follow here.  
While the dot notation is also available for named tuples, I only know their definition as (name1 = val1, ).  
How get the nested functions _attached_ to the outer function?

Don’t want to be too fussy, if it’s just the way it works, ok. Maybe there is something basic to learn.

---

<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: [October 26, 2021, 11:01am UTC](https://discourse.julialang.org/t/do-julia-structs-have-member-functions-and-this/8839/25 "2021-10-26T11:01:31Z")

</div>

> [@Bardo](#):
>
> While the dot notation is also available for named tuples, I only know their definition as (name1 = val1, ).  
> How get the nested functions _attached_ to the outer function?

They’re not attached at all! 😃 They’re defined in `Foo` and capture the variable `x`, i.e. they keep a reference to `x`. Finally, the last anonymous function captures (keeps a reference) to both `getx` and `setx`, which can be retrieved due to the trick of having them saved in a named tuple. Finally, `Foo()().getx` is the same as `f = Foo(); vars = f(); getx_func = vars.getx; getx_func()`. Internally, it’s a little more complicated because anonymous functions are basically callable structs, which are defined before `Foo` during lowering - but that’s just an implementation detail.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [October 26, 2021, 11:42am UTC](https://discourse.julialang.org/t/do-julia-structs-have-member-functions-and-this/8839/26 "2021-10-26T11:42:10Z")

</div>

I think it’s fine to find this confusing. `(getx;setx)` does not create a named tuple, it just returns `setx`. With a recent enough Julia `(;getx, setx)` does create a named tuple though. Compare these implementations:

```julia
julia> function Foo1()
             x::Int = -1
             setx(x_) = (x = x_) 
             getx() = x
             () -> (getx;setx)
       end
Foo1 (generic function with 1 method)

julia> function Foo2()
             x::Int = -1
             setx(x_) = (x = x_) 
             getx() = x
             () -> (;getx, setx)
       end
Foo2 (generic function with 1 method)

julia> f1=Foo1()
#27 (generic function with 1 method)

julia> f2=Foo2()
#31 (generic function with 1 method)

julia> f2().getx()
-1

julia> f1().getx()
ERROR: type #setx#28 has no field getx
Stacktrace:
 [1] getproperty(x::Function, f::Symbol)
   @ Base ./Base.jl:33
 [2] top-level scope
   @ REPL[58]:1

```

However, the magic of the original example is that you in both cases can do

```julia
julia> f2.getx()
-1

julia> f1.getx()
-1

```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [October 26, 2021, 1:59pm UTC](https://discourse.julialang.org/t/do-julia-structs-have-member-functions-and-this/8839/27 "2021-10-26T13:59:00Z")

</div>

Related: [How to understand this code that seemingly creates an anonymous type on the fly? - #2 by rdeits](https://discourse.julialang.org/t/how-to-understand-this-code-that-seemingly-creates-an-anonymous-type-on-the-fly/69636/2)

---

<div class="post-metadata">

### Author: ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)
#### Post date: [November 19, 2021, 7:19am UTC](https://discourse.julialang.org/t/do-julia-structs-have-member-functions-and-this/8839/28 "2021-11-19T07:19:31Z")

</div>

I like the explicit syntax using named tuples!

```julia
function Foo()
    x = -1 # should make it Int
    setx(xnew) = (x = Int(xnew)) 
    getx() = x
    (; getx, setx)
end

julia> f = Foo()
(getx = var"#getx#18"(Core.Box(-1)), setx = var"#setx#17"(Core.Box(-1)))

```

Is the Box(ing) above a bad sign for performance?  
The compiler should be informed enough about types. Why does it appear?

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [November 19, 2021, 7:56am UTC](https://discourse.julialang.org/t/do-julia-structs-have-member-functions-and-this/8839/29 "2021-11-19T07:56:46Z")

</div>

The absolute horror of that.

[Previous page](https://discourse.julialang.org/t/do-julia-structs-have-member-functions-and-this/8839.md?page=1)
