# When and How to use Types

**URL:** <https://discourse.julialang.org/t/when-and-how-to-use-types/56659>\
**Category:** General Usage\
**Tags:** question, performance, parametric-types\
**Created:** [March 7, 2021, 7:33am UTC](https://discourse.julialang.org/t/when-and-how-to-use-types/56659 "2021-03-07T07:33:03Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 8, 2021, 9:05pm UTC](https://discourse.julialang.org/t/when-and-how-to-use-types/56659/5 "2021-03-08T21:05:18Z")

</div>

Maybe [Memoize.jl](https://github.com/JuliaCollections/Memoize.jl) is useful to you.

Anyway, I would probably use a more pedestrian approach, by computing keeping `r` as a parameter and adding the constant terms, precomputed, to a data structure to be sent to the function as a parameter as well. But of course how practical is that depends on your actual problem.

One alternative which I find cool, although I am not sure if that will fit your needs either, is to use a functor, something like this:

The struct is parametric on the constants, and the constructor computes the constants

```julia
julia> struct Test{M,D}
         c1::Float64
         c2::Float64
         Test(m,d) = new{m,d}( m*d, m/d )
       end

```

Define a `functor`, which is a function that makes the objects of type `Test` callable, depending on the constants:

```julia
julia> function (t::Test{M,D})(r) where {M,D}
         t.c1*r - t.c2*r
       end

```

When you define an instance of the type `Test`, you get the constants:

```julia
julia> t = Test(3,4)
Test{3,4}(12.0, 0.75)

```

And this object can be called with different `r`, to compute the result:

```julia
julia> t(2.5)
28.125

```

---

_[View the full topic](https://discourse.julialang.org/t/when-and-how-to-use-types/56659)._
