# Function name with Type constraining

**URL:** <https://discourse.julialang.org/t/function-name-with-type-constraining/121635>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 23, 2024, 12:33pm UTC](https://discourse.julialang.org/t/function-name-with-type-constraining/121635 "2024-10-23T12:33:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)\
**Post date:** [October 23, 2024, 12:33pm UTC](https://discourse.julialang.org/t/function-name-with-type-constraining/121635/1 "2024-10-23T12:33:11Z")

</div>

I met code as follow, anyone can give an syntactic explanation?

```Julia
mutable struct WriteClosure{T, KW}
    buf::T
end
function (f::WriteClosure)(i, nm, TT, v; kw...)
end

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 23, 2024, 1:01pm UTC](https://discourse.julialang.org/t/function-name-with-type-constraining/121635/2 "2024-10-23T13:01:24Z")

</div>

If an object of type `WriteClosure` is called, then that function is called.

Example:

```julia
> wc = WriteClosure{T1, T2}(buf)
> wc(i, nm, TT, v) # calling the defined function

```

---

<div class="post-metadata">

**Author:** ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)\
**Post date:** [October 25, 2024, 6:06am UTC](https://discourse.julialang.org/t/function-name-with-type-constraining/121635/3 "2024-10-25T06:06:27Z")

</div>

Thanks!  
Well, is it a common syntax, and what’s the name ?

---

<div class="post-metadata">

**Author:** ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)\
**Post date:** [October 25, 2024, 6:23am UTC](https://discourse.julialang.org/t/function-name-with-type-constraining/121635/4 "2024-10-25T06:23:36Z")

</div>

The relevant part of the Julia docs is [here](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects).  
I’d say they are a commonly used feature of the language. Sometimes, they are referred to as “Functor” or “callable struct.”

---

<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:** [October 25, 2024, 7:07am UTC](https://discourse.julialang.org/t/function-name-with-type-constraining/121635/5 "2024-10-25T07:07:25Z")

</div>

Method dispatch does not treat function-like objects differently from typical functions. Say in the call `foo(a, b)`, the dispatch is occurring over the types of the callable and the arguments `(foo, a, b) isa Tuple{typeof(foo), typeof(a), typeof(b)}`. In fact, you can add methods to `foo` with the same syntax `(::typeof(foo))(c) = "blah"`, but typical functions are conveniently given a `const` name and extra behaviors via subtyping `Function`. If the `struct` type isn’t singleton (has no fields), then you can’t give any of its instances a `const` name, but otherwise you could opt into much of function’s behaviors.

---

<div class="post-metadata">

**Author:** ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)\
**Post date:** [October 28, 2024, 7:40am UTC](https://discourse.julialang.org/t/function-name-with-type-constraining/121635/6 "2024-10-28T07:40:41Z")

</div>

Thanks!  
I should read the docs more often.
