How to understand this "typemaker" function?

This example is somewhat contrived…

It works by creating a closure which is an anonymous function. 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.

3 Likes