# Redefining a method used in a generated function

**URL:** <https://discourse.julialang.org/t/redefining-a-method-used-in-a-generated-function/35225>\
**Category:** General Usage\
**Created:** [February 27, 2020, 12:15pm UTC](https://discourse.julialang.org/t/redefining-a-method-used-in-a-generated-function/35225 "2020-02-27T12:15:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sheemon7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sheemon7/32/10277_2.png) [@Sheemon7](https://discourse.julialang.org/u/Sheemon7)\
**Post date:** [February 27, 2020, 12:15pm UTC](https://discourse.julialang.org/t/redefining-a-method-used-in-a-generated-function/35225/1 "2020-02-27T12:15:15Z")

</div>

I understand that generated functions are generated “on demand” and therefore all used methods have to be defined before:

```julia
julia> @generated g() = (@show f(1); nothing)
g (generic function with 1 method)

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

julia> g()
ERROR: MethodError: no method matching f(::Int64)

```

However, why it is not possible to **redefine** methods like this?

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

julia> @generated g() = (@show f(1); nothing)
g (generic function with 1 method)

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

julia> g()
f(1) = 1

```

Is there a way around this or is it just fundamentally impossible? I am writing a library, where I would like to generate functions based on type traits and this would really come handy.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [February 27, 2020, 12:22pm UTC](https://discourse.julialang.org/t/redefining-a-method-used-in-a-generated-function/35225/2 "2020-02-27T12:22:07Z")

</div>

No, this is not safe. Although, it often works, there are no guarantees. Some discussion here: [https://github.com/mauro3/SimpleTraits.jl/issues/40](https://github.com/mauro3/SimpleTraits.jl/issues/40)

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 27, 2020, 2:06pm UTC](https://discourse.julialang.org/t/redefining-a-method-used-in-a-generated-function/35225/3 "2020-02-27T14:06:57Z")

</div>

> [@mauro3](#):
>
> Although, it often works, there are no guarantees

It is garanteed to not work. If it does then it’s a bug.
