# What am I missing in these codes?

**URL:** <https://discourse.julialang.org/t/what-am-i-missing-in-these-codes/87347>\
**Category:** New to Julia\
**Created:** [September 16, 2022, 5:37am UTC](https://discourse.julialang.org/t/what-am-i-missing-in-these-codes/87347 "2022-09-16T05:37:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [September 16, 2022, 5:37am UTC](https://discourse.julialang.org/t/what-am-i-missing-in-these-codes/87347/1 "2022-09-16T05:37:06Z")

</div>

Code1:

```julia
f1(x::Int)=println("1: $x")
Int(x)=println("int: $x")
f2(x::Int)=println("2: $x")

f2 (generic function with 1 method)

```

Code2:

```julia
julia> Int(x)=println("int: $x")
Int (generic function with 1 method)

julia> f2(x::Int)=println("2: $x")
ERROR: ArgumentError: invalid type for argument x in method definition for f2 at REPL[2]:1
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

```

Code1 works fine, while Code2 not. It seems like `method Int` must follow something, which showing `Int` is a `Type`, then the intepreter can infer `Int` is a `Type` rather than a `method`?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [September 16, 2022, 7:34am UTC](https://discourse.julialang.org/t/what-am-i-missing-in-these-codes/87347/2 "2022-09-16T07:34:20Z")

</div>

I’m not sure what causes your specific error, but I’m not surprised you have a bad time redefining the fallback constructor for `Int`.

Actually hang on. The error mentions `f1`, which is not in “Code 2”. Is anything left out?

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [September 16, 2022, 7:41am UTC](https://discourse.julialang.org/t/what-am-i-missing-in-these-codes/87347/3 "2022-09-16T07:41:07Z")

</div>

Execuse me, it is a copy mistake, I have rewrited it.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [September 16, 2022, 7:51am UTC](https://discourse.julialang.org/t/what-am-i-missing-in-these-codes/87347/4 "2022-09-16T07:51:15Z")

</div>

By defining `Int(x)` (before every using/accessing the type `Int`) you are overshadowing what `Int` usually refers to (namely `Core.Int`). After you definition `Int` isn’t the standard integer datatype anymore but your function. And you can’t use `x::Int` the the function signature of `f2` because that’s essentially `x::some_function` which isn’t valid.

Compare

```julia
julia> Int
Int64

julia> typeof(Int)
DataType

```

to (in a fresh session)

```julia
julia> Int(x) = println("int: $x")
Int (generic function with 1 method)

julia> Int
Int (generic function with 1 method)

julia> typeof(Int)
typeof(Int) (singleton type of function Int, subtype of Function)

```

(Note that you could, in principle, do `f2(x::Core.Int) = ...`)

Most importantly, though, you shouldn’t do this anyways. `Int(x)` already has a meaning and it’s not to print something.
