# Warning: Method definition f(Any) overwritten

**URL:** https://discourse.julialang.org/t/warning-method-definition-f-any-overwritten/103060
**Category:** New to Julia
**Created:** [August 22, 2023, 12:12pm UTC](https://discourse.julialang.org/t/warning-method-definition-f-any-overwritten/103060 "2023-08-22T12:12:11Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [August 22, 2023, 8:01pm UTC](https://discourse.julialang.org/t/warning-method-definition-f-any-overwritten/103060/7 "2023-08-22T20:01:57Z")

</div>

You want an anonymous function:

Simple solution:

```julia
function create_f(flag::Bool)
  if flag
    x -> x^2
  else
    x -> x^3
  end
end

```

The above solution is, however, not type stable:

```julia-repl
julia> using Test

julia> @inferred create_f(true)
ERROR: return type var"#1#3" does not match inferred return type Union{var"#1#3", var"#2#4"}

```

A type stable solution:

```julia
function create_f(flag::Bool)
  m = flag ? 2 : 3
  let n = m
    x -> x^n
  end
end

```

Now the type is uniquely inferred:

```julia-repl
julia> using Test

julia> @inferred create_f(true)
#1 (generic function with 1 method)

```

---

_[View the full topic](https://discourse.julialang.org/t/warning-method-definition-f-any-overwritten/103060)._
