# How to understand this "typemaker" function?

**URL:** <https://discourse.julialang.org/t/how-to-understand-this-typemaker-function/43014>\
**Category:** New to Julia\
**Created:** [July 14, 2020, 2:26am UTC](https://discourse.julialang.org/t/how-to-understand-this-typemaker-function/43014 "2020-07-14T02:26:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zhong\_Pan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhong_pan/32/19872_2.png) [@Zhong\_Pan](https://discourse.julialang.org/u/Zhong_Pan)\
**Post date:** [July 14, 2020, 2:26am UTC](https://discourse.julialang.org/t/how-to-understand-this-typemaker-function/43014/1 "2020-07-14T02:26:23Z")

</div>

I read a post on towardsdatascience:  
[https://towardsdatascience.com/3-smooth-syntactical-tips-for-julia-65ddf577f071](https://towardsdatascience.com/3-smooth-syntactical-tips-for-julia-65ddf577f071)

It used something that looks to me like an anonymous type, which I don’t know exists:

```julia
mutable struct car_stats
    distance
    time
    speed
end
honda_civic = car_stats(25, 40, nothing)

function speed(carstat)
    mph = carstat.distance / carstat.time
    return(mph)
end

typemaker(speedfunc,civic) = ()->(speedfunc;civic)

d = typemaker(speed,honda_civic)

d.speedfunc(d.civic)

```

The question is, why would this even work? What is the anonymous function’s return value (speedfunc;civic)? Why a function that returns this thing can be used as if it’s a user defined type (struct)?

An explanation and/or link to the relevant document will be appreciated!

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [July 14, 2020, 2:41am UTC](https://discourse.julialang.org/t/how-to-understand-this-typemaker-function/43014/2 "2020-07-14T02:41:13Z")

</div>

This example is somewhat contrived…

It works by creating a [closure](https://docs.julialang.org/en/v1/devdocs/functions/#Closures-1) which is an [anonymous function](https://docs.julialang.org/en/v1/manual/functions/index.html#man-anonymous-functions-1). Internally, anonymous functions capture the various variables referenced within.

Here even though the anonymous function `()->(speedfunc;civic)` simply returns `civic` (it doesn’t do anything with `speedfunc`), both variables are still captured (and accessible by dot notation, like structs).

For this example, there is no reason to have the `typemaker` function which _returns_ an anonymous function. All of the info is already captured in the type `car_stats`. Instead of calling `d.speedfunc(d.civic)`, just call `speed(honda_civic)`…

If there is a need for some object to store the speedfunc and the car, as is done by the closure, a better way would be to simply create a tuple `(speedfunc,civic)`, or simply create another type.

---

<div class="post-metadata">

**Author:** ![Zhong\_Pan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhong_pan/32/19872_2.png) [@Zhong\_Pan](https://discourse.julialang.org/u/Zhong_Pan)\
**Post date:** [July 14, 2020, 3:11am UTC](https://discourse.julialang.org/t/how-to-understand-this-typemaker-function/43014/3 "2020-07-14T03:11:47Z")

</div>

Thank you! This is quite interesting…
