# Defining a type that behaves like a (const) function

**URL:** <https://discourse.julialang.org/t/defining-a-type-that-behaves-like-a-const-function/37162>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 7, 2020, 12:34pm UTC](https://discourse.julialang.org/t/defining-a-type-that-behaves-like-a-const-function/37162 "2020-04-07T12:34:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [April 7, 2020, 12:34pm UTC](https://discourse.julialang.org/t/defining-a-type-that-behaves-like-a-const-function/37162/1 "2020-04-07T12:34:02Z")

</div>

Dear community,

I’m trying to define a type that behaves as a function. The usual way is something like

```julia
struct T
    z::Int
end

(t::T)(x) = x * t.z 

```

Then, if `t = T(2)` we have `t(3) == 6`.

However there is one way in which `t` is _not_ like a function. It is not a `const`. Hence, if I do `f(x) = t(x)`, then `f` is not inferred (it relies on a non-const global)

```julia
julia> @code_warntype f(2)
Variables
  #self#::Core.Compiler.Const(f, false)
  x::Int64

Body::Any
1 ─ %1 = Main.t(x)::Any
└── return %1

```

In contrast, `t(2)` infers fine. If `t` were a regular function, not just a callable type, `f` would be inferred, because functions are not only callable, they are `const`s.

The question: is there any way to define `T` so that `t = T(2)` behaves as `const t = T(2)` (like when one defines a regular function), without requiring that the user specify that `const`?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [April 7, 2020, 12:40pm UTC](https://discourse.julialang.org/t/defining-a-type-that-behaves-like-a-const-function/37162/2 "2020-04-07T12:40:36Z")

</div>

The reason, why this doesn’t infer is probably that `t` is a non-constant global, which is used by `f`. If you pass `t` to `f` as an argument or define it inside of `f`, this should infer just fine.

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [April 7, 2020, 12:43pm UTC](https://discourse.julialang.org/t/defining-a-type-that-behaves-like-a-const-function/37162/3 "2020-04-07T12:43:18Z")

</div>

Hi @simeonschaub, yes, I understand that. I just wanted to know if I can make `f(x) = t(x)` inferable, just like `f(x) = sin(x)` is inferable, without declaring `t` as a const or passing it as an extra variable. I suspect there is probably no way (after all, anonymous function bindings are not const by default), but wanted to check anyhow.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [April 7, 2020, 12:45pm UTC](https://discourse.julialang.org/t/defining-a-type-that-behaves-like-a-const-function/37162/4 "2020-04-07T12:45:24Z")

</div>

It depends on what exactly you want to do. You can declare `T` a mutable struct and make `t` a constant. You can then still change `t.z` with `t.z = 42`, but everything will be type stable.
