gTcV
gTcV

Reputation: 2504

Easy way to get world age of function in Julia?

I just learnt about world age in Julia and must say this concept will take some time to sink in. In order to help me coming to terms with it, it might be useful to be able to explicit see the world age in some examples. Is there an easy way to do this?

I guess I could somehow make my examples such that they always run into a world age error, but I was hoping there is a less mind-boggling way to achieve this.

Upvotes: 2

Views: 325

Answers (1)

Dan Getz
Dan Getz

Reputation: 18227

The following is hack-ish, and works on 0.6.1, but will probably break later:

min_world(f) = (methods(f)).ms[1].min_world

macro cur_world()
    :(min_world(x -> $(rand())*x))
end

And it works as follows:

julia> f(x) = 2x
f (generic function with 1 method)

julia> @cur_world()
21827

julia> g(x) = sqrt(x)
g (generic function with 1 method)

julia> @cur_world()
21829

julia> min_world(f)
21826

julia> min_world(g)
21828

Using the world-age counter in any real code sounds like the wrong way to go.

Upvotes: 2

Related Questions