# Explain the syntax to use an instantiated struct name as a method?

**URL:** <https://discourse.julialang.org/t/explain-the-syntax-to-use-an-instantiated-struct-name-as-a-method/86484>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 29, 2022, 8:18am UTC](https://discourse.julialang.org/t/explain-the-syntax-to-use-an-instantiated-struct-name-as-a-method/86484 "2022-08-29T08:18:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scoopxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scoopxyz/32/25177_2.png) [@scoopxyz](https://discourse.julialang.org/u/scoopxyz)\
**Post date:** [August 29, 2022, 8:18am UTC](https://discourse.julialang.org/t/explain-the-syntax-to-use-an-instantiated-struct-name-as-a-method/86484/1 "2022-08-29T08:18:50Z")

</div>

Hello,

I came across a interesting way of calling a instantiated struct directly (not initializing) as a method when looking at the **Distances.jl** module:

```julia
dist = DistanceType(options)
output = dist(x1, x2)

```

I had no idea this was possible! Looking at another post [here](https://discourse.julialang.org/t/calling-a-struct-as-a-function/4911/2) on Discourse I found how to implement it with this syntax:

```julia
function (a::MyStruct)(param1, param2)
...
end

```

It clearly works, but I’m having a hard time understanding how. How does the above allow for dispatching on the `MyStruct` type without it being one of the parameters? It even allows you to access the `MyStruct` fields as `a.foo`

Any insight is appreciated.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 29, 2022, 8:37am UTC](https://discourse.julialang.org/t/explain-the-syntax-to-use-an-instantiated-struct-name-as-a-method/86484/2 "2022-08-29T08:37:20Z")

</div>

To be precise, you are calling an instance of a struct. This is documented in the [Function-like objects](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects) section. A function call `f(a, b, c)` dispatches a method based on the types of _the called function_ `f` and the arguments `a, b, c`. It’s just that a function is an instance of a singleton type; that’s why `typeof(f)` is simply printed as `typeof(f)`. A `function` definition can also make a method that is dispatched based on a non-singleton type of _called instances_. Despite the definition name, the type is not necessarily a function type; that depends on if it subtypes `Function`.

Unlike arguments, there are restrictions on what you can annotate the called instance in the method definition. As you know, no annotations `function f(...` makes a singleton function type. Concrete annotations `function (m::Int)(...` are okay. Some abstract annotations, like types you defined, `function (m::Number)(...` are okay. Overly general annotations like `::Function`, `::Any`, `::Type`, or `::DataType` will throw an error or cause bugs; I don’t know the exact criteria or an exhaustive list.

About calling a type, e.g. `DistanceType(options)`. Many types, like `struct` types, are instances of `DataType`, but Julia specializes on the abstract singleton [`Type{T}`](https://docs.julialang.org/en/v1/manual/types/#man-typet-type) in many circumstances for type stability. I’m not really sure how to describe the circumstances.

---

<div class="post-metadata">

**Author:** ![tsela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsela/32/52174_2.png) [@tsela](https://discourse.julialang.org/u/tsela)\
**Post date:** [August 29, 2022, 8:38am UTC](https://discourse.julialang.org/t/explain-the-syntax-to-use-an-instantiated-struct-name-as-a-method/86484/3 "2022-08-29T08:38:30Z")

</div>

This: [Methods · The Julia Language](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects) part of the Julia documentation deals with such definitions, which are often called “functors”. Searching for “Julia functors” will lead to many more explanations of the syntax and its uses.

---

<div class="post-metadata">

**Author:** ![scoopxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scoopxyz/32/25177_2.png) [@scoopxyz](https://discourse.julialang.org/u/scoopxyz)\
**Post date:** [August 29, 2022, 8:44am UTC](https://discourse.julialang.org/t/explain-the-syntax-to-use-an-instantiated-struct-name-as-a-method/86484/4 "2022-08-29T08:44:06Z")

</div>

Thank you both! The Julia documentation and the term “functors” will help me dig further.

@Benny ah ok, right, the methods themselves are also typed this makes sense. So its all rolled under the big multiple dispatch umbrella. Excuse me while my brain stretches in new directions to understand.

Thanks!
