# \[SOLVED\] UInt is not Int!

**URL:** <https://discourse.julialang.org/t/solved-uint-is-not-int/40038>\
**Category:** New to Julia\
**Created:** [May 23, 2020, 7:31pm UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038 "2020-05-23T19:31:10Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![lhp22](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lhp22](https://discourse.julialang.org/u/lhp22)\
**Post date:** [May 23, 2020, 7:31pm UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038/1 "2020-05-23T19:31:10Z")

</div>

Hi !

I’ve encountered something curious, and here is a minimal example 🙂

```julia
function f(x::UInt,y::UInt)::UInt
    x + y 
end

```

And now i want to do `f(2,2)` but julia says this to me :

```julia
julia> f(2,2)
ERROR: MethodError: no method matching f(::Int64, ::Int64)
Stacktrace:
 [1] top-level scope at REPL[1]:1

```

For a function, I need just positive numbers, no I want to force this by the typing. Is a there a way to do this ?

In some sense, why `f(0x2,0x2)` fails ? It says to me that there is not matching for `f(::UInt8,::UInt8)` … but `UInt8 <: UInt` is true, isn’t it ?

Thanks for answers 🙂

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [May 23, 2020, 7:43pm UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038/2 "2020-05-23T19:43:18Z")

</div>

As you’ve noticed, `2` is cast to `Int`, which on a 64-bit system is `Int64`.

`0x2` is `UInt8`. This is NOT `<: UInt`, though, since `UInt` is `UInt64` on 64-bit systems.

Just call it with `UInt(...)`: `f(UInt(2), UInt(2))`, or coerce inside the function:

```julia
julia> function f(x::Integer, y::Integer)
         ux = UInt(x); uy = UInt(y)
         ux + uy
       end
f (generic function with 1 method)
       
julia> f(3, 5)
0x0000000000000008

julia> f(-3, 5)
ERROR: InexactError: check_top_bit(UInt64, -3)
Stacktrace:
 [1] throw_inexacterror(::Symbol, ::Type{UInt64}, ::Int64) at ./boot.jl:557
 [2] check_top_bit at ./boot.jl:571 [inlined]
 [3] toUInt64 at ./boot.jl:682 [inlined]
 [4] UInt64 at ./boot.jl:712 [inlined]
 [5] f(::Int64, ::Int64) at ./REPL[5]:2
 [6] top-level scope at REPL[7]:1

```

edited to add: I wrote a quick script that will tell me the entire type hierarchy for an object:

```julia
function typehier(x)
    z = typeof(x)
    print("$z")
    while z != Any
        z = supertype(z)
        print(" <: $z")
    end
    println
end

```

```julia
julia> typehier(5)
Int64 <: Signed <: Integer <: Real <: Number <: Any

julia> typehier(0x5)
UInt8 <: Unsigned <: Integer <: Real <: Number <: Any

julia> typehier("foo")
String <: AbstractString <: Any

```

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [May 23, 2020, 7:45pm UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038/3 "2020-05-23T19:45:03Z")

</div>

> [@lhp22](#):
>
> but `UInt8 <: UInt` is true, isn’t it ?

Try it out:

```julia
julia> UInt8 <: UInt
false

```

Maybe you are looking for `Unsigned` instead? That’s the supertype of all unsigned integers:

```julia
julia> UInt8 <: Unsigned
true

```

Now even if you define `f(x::Unsigned, y::Unsigned)` this will still not allow you to call `f(2, 2)` because `2` is a `Int` – a signed integer. The julia way would be something like this:

```julia
f(x::Unsigned, y::Unsigned) = x + y
f(x, y) =f(convert(Unsigned, x), convert(Unsigned, y))

```

In other words, you define the function for `Unsigned` values, and you also specify that if it is passed anything else, those things should be converted to `Unsigned` and then passed to the original definition.

---

<div class="post-metadata">

**Author:** ![lhp22](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lhp22](https://discourse.julialang.org/u/lhp22)\
**Post date:** [May 23, 2020, 8:00pm UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038/4 "2020-05-23T20:00:55Z")

</div>

Ok.

1/ I’ve a problem with the number system types. I should read the doc again.

2/ Thanks for your answers to you both ! I’ve already thought to the first, but it seemed to me strange. The second is more julia-like.

Thanks for your quick answers !

---

<div class="post-metadata">

**Author:** ![jbrea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbrea/32/3879_2.png) [@jbrea](https://discourse.julialang.org/u/jbrea)\
**Post date:** [May 23, 2020, 8:22pm UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038/5 "2020-05-23T20:22:28Z")

</div>

> [@lhp22](#):
>
> For a function, I need just positive numbers, no I want to force this by the typing.

Why do you want to enforce this with the type? Types in the function signature are great for multiple dispatch, but I think a more common pattern for argument checking is something like

```julia
function f(x, y)
    x <= 0 && error("x should be positive.")
    y <= 0 && error("y should be positive.")
    x + y
end

```

See e.g. `sqrt` in base/math.jl

```julia
@inline function sqrt(x::Union{Float32,Float64})
    x < zero(x) && throw_complex_domainerror(:sqrt, x)
    sqrt_llvm(x)
end

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [May 23, 2020, 8:42pm UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038/6 "2020-05-23T20:42:18Z")

</div>

Note that the generic `error` function isn’t a good fit here since it obscures the actual error in a generic message. Contrast that with the `sqrt` function, which properly throws a `DomainError`:

```julia
julia> using Test

julia> @test_throws DomainError sqrt(-1)
Test Passed
      Thrown: DomainError

```

which is much easier to explicitly catch and handle. If you want to restrict the function, you can first check the arguments, convert afterwards and work with the converted arguments to get the maximum out of the compiler.

---

<div class="post-metadata">

**Author:** ![lhp22](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lhp22](https://discourse.julialang.org/u/lhp22)\
**Post date:** [May 23, 2020, 8:50pm UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038/7 "2020-05-23T20:50:59Z")

</div>

Because I have a complex type system ? It should be unnice if I couldn’t use it for easing my code 🙂

---

<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:** [May 23, 2020, 9:03pm UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038/8 "2020-05-23T21:03:47Z")

</div>

Having a complex type system does not mean you have to or should use it for everything. In fact, using it to restrict user input is almost always bad. The user will have a **MUCH** better time understanding the error message from `sqrt(-1)` than from your function.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 23, 2020, 9:28pm UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038/9 "2020-05-23T21:28:40Z")

</div>

If you don’t want to, don’t put types on your function signatures. It’s not required or even helpful for performance. You can write Julia code without types just like you would Python, R or Matlab.

---

<div class="post-metadata">

**Author:** ![dataSurfer](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@dataSurfer](https://discourse.julialang.org/u/dataSurfer)\
**Post date:** [May 24, 2020, 3:08am UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038/10 "2020-05-24T03:08:16Z")

</div>

> [@StefanKarpinski](#):
>
> You can write Julia code without types just like you would Python, R or Matlab.

[**!Warning Pendantic!**] Yes but isn’t there a caveat that if you leave out types from structs and data objects altogether it will lead to a degradation in performance where the compiler starts substituting `Any`? For example this:

```julia
struct MyTypedStruct{T}
  x::T
end

```

Compared with this:

```julia
struct MyUnTypedStruct
x
end

```

The second implementation will give `Any` warnings when passed into `@code_warntype`:

```julia
function fun(x)
  return x.x^2 + 2
end

```

```julia
@code_warntype fun(MyTypedStruct(3.0))

```

Creates good code:

```julia
Variables
  #self#::Core.Compiler.Const(fun, false)
  x::MyTypedStruct{Float64}

Body::Float64
1 ─ %1 = Base.getproperty(x, :x)::Float64
│ %2 = Core.apply_type(Base.Val, 2)::Core.Compiler.Const(Val{2}, false)
│ %3 = (%2)()::Core.Compiler.Const(Val{2}(), false)
│ %4 = Base.literal_pow(Main.:^, %1, %3)::Float64
│ %5 = (%4 + 2)::Float64
└── return %5

```

```julia
@code_warntype fun(MyUnTypedStruct(3.0))

```

Creates code with `Any` type identifiers that will lead to performance penalties:

```julia
Variables
  #self#::Core.Compiler.Const(fun, false)
  x::MyUnTypedStruct

Body::Any
1 ─ %1 = Base.getproperty(x, :x)::Any
│ %2 = Core.apply_type(Base.Val, 2)::Core.Compiler.Const(Val{2}, false)
│ %3 = (%2)()::Core.Compiler.Const(Val{2}(), false)
│ %4 = Base.literal_pow(Main.:^, %1, %3)::Any
│ %5 = (%4 + 2)::Any
└── return %5

```

To beginner programmers that may be reading, this might not look like much but when you use `@code_llvm`, the well defined type creates really good efficient small code size:

```julia
@code_llvm fun(MyTypedStruct(3.0))

; @ REPL[3]:2 within `fun'
define double @julia_fun_17308([1 x double] addrspace(11)* nocapture nonnull readonly dereferenceable(8)) {
top:
; ┌ @ Base.jl:33 within `getproperty'
   %1 = getelementptr inbounds [1 x double], [1 x double] addrspace(11)* %0, i64 0, i64 0
; └
; ┌ @ intfuncs.jl:261 within `literal_pow'
; │┌ @ float.jl:405 within `*'
    %2 = load double, double addrspace(11)* %1, align 8
    %3 = fmul double %2, %2
; └└
; ┌ @ promotion.jl:311 within `+' @ float.jl:401
   %4 = fadd double %3, 2.000000e+00
; └
  ret double %4
}

```

whereas untyped code creates a horribly inefficient monstrosity:

```julia
@code_llvm fun(MyUnTypedStruct(3.0))

; @ REPL[3]:2 within `fun'
define nonnull %jl_value_t addrspace(10)* @japi1_fun_17309(%jl_value_t addrspace(10)*, %jl_value_t addrspace(10)**, i32) #0 {
top:
  %3 = alloca %jl_value_t addrspace(10)*, i32 3
  %gcframe = alloca %jl_value_t addrspace(10)*, i32 3, align 16
  %4 = bitcast %jl_value_t addrspace(10)** %gcframe to i8*
  call void @llvm.memset.p0i8.i32(i8* align 16 %4, i8 0, i32 24, i1 false)
  %5 = alloca %jl_value_t addrspace(10)**, align 8
  store volatile %jl_value_t addrspace(10)**%1, %jl_value_t addrspace(10)*** %5, align 8
  %thread_ptr = call i8* asm "movq %fs:0, $0", "=r"()
  %ptls_i8 = getelementptr i8, i8* %thread_ptr, i64 -15712
  %ptls = bitcast i8* %ptls_i8 to %jl_value_t***
  %6 = getelementptr %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)** %gcframe, i32 0
  %7 = bitcast %jl_value_t addrspace(10)** %6 to i64*
  store i64 4, i64* %7
  %8 = getelementptr %jl_value_t **, %jl_value_t*** %ptls, i32 0
  %9 = getelementptr %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)** %gcframe, i32 1
  %10 = bitcast %jl_value_t addrspace(10) **%9 to %jl_value_t***
  %11 = load %jl_value_t **, %jl_value_t*** %8
  store %jl_value_t **%11, %jl_value_t*** %10
  %12 = bitcast %jl_value_t***%8 to %jl_value_t addrspace(10)***
  store %jl_value_t addrspace(10)**%gcframe, %jl_value_t addrspace(10)*** %12
  %13 = load %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)** %1, align 8
; ┌ @ Base.jl:33 within `getproperty'
   %14 = addrspacecast %jl_value_t addrspace(10)* %13 to %jl_value_t addrspace(11)*
   %15 = bitcast %jl_value_t addrspace(11)* %14 to %jl_value_t addrspace(10)* addrspace(11)*
   %16 = load %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)* addrspace(11)* %15, align 8
; └
  %17 = getelementptr %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)** %3, i32 0
  store %jl_value_t addrspace(10)* addrspacecast (%jl_value_t* inttoptr (i64 140357750891408 to %jl_value_t*) to %jl_value_t addrspace(10)*), %jl_value_t addrspace(10)** %17
  %18 = getelementptr %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)** %3, i32 1
  store %jl_value_t addrspace(10)* %16, %jl_value_t addrspace(10)** %18
  %19 = getelementptr %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)** %3, i32 2
  store %jl_value_t addrspace(10)* addrspacecast (%jl_value_t* inttoptr (i64 140357711681584 to %jl_value_t*) to %jl_value_t addrspace(10)*), %jl_value_t addrspace(10)** %19
  %20 = call nonnull %jl_value_t addrspace(10)* @jl_apply_generic(%jl_value_t addrspace(10)* addrspacecast (%jl_value_t* inttoptr (i64 140357755649744 to %jl_value_t*) to %jl_value_t addrspace(10)*), %jl_value_t addrspace(10)** %3, i32 3)
  %21 = getelementptr %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)** %gcframe, i32 2
  store %jl_value_t addrspace(10)* %20, %jl_value_t addrspace(10)** %21
  %22 = getelementptr %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)** %3, i32 0
  store %jl_value_t addrspace(10)* %20, %jl_value_t addrspace(10)** %22
  %23 = getelementptr %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)** %3, i32 1
  store %jl_value_t addrspace(10)* addrspacecast (%jl_value_t* inttoptr (i64 140357616279712 to %jl_value_t*) to %jl_value_t addrspace(10)*), %jl_value_t addrspace(10)** %23
  %24 = call nonnull %jl_value_t addrspace(10)* @jl_apply_generic(%jl_value_t addrspace(10)* addrspacecast (%jl_value_t* inttoptr (i64 140357742243008 to %jl_value_t*) to %jl_value_t addrspace(10)*), %jl_value_t addrspace(10)** %3, i32 2)
  %25 = getelementptr %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)** %gcframe, i32 1
  %26 = load %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)** %25
  %27 = getelementptr %jl_value_t **, %jl_value_t*** %ptls, i32 0
  %28 = bitcast %jl_value_t***%27 to %jl_value_t addrspace(10)**
  store %jl_value_t addrspace(10)* %26, %jl_value_t addrspace(10)** %28
  ret %jl_value_t addrspace(10)* %24
}

```

One of Julia’s selling points is performance and another is the ability to write code as if you were doing so using a static programming language with all the benefits of a dynamic language so it shouldn’t come as a surprise that people want to use the type system and get all the performance benefits. My personal feeling is that there’s nothing wrong with using types it constrains the code your intent and if all your Julia code is typed you avoid performance penalties like the one described above.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 24, 2020, 3:09am UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038/11 "2020-05-24T03:09:04Z")

</div>

Sure, but no worse than those languages.

---

<div class="post-metadata">

**Author:** ![dataSurfer](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@dataSurfer](https://discourse.julialang.org/u/dataSurfer)\
**Post date:** [May 24, 2020, 3:20am UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038/12 "2020-05-24T03:20:31Z")

</div>

Yes, but Julia is better. Having a type system in a dynamic language that creates efficient code is mind-blowing 🤯. You couldn’t write something like this in any other language; [Julia parser and writer for the IDX file format used by e.g. the MNIST dataset · GitHub](https://gist.github.com/Godisemo/e0c02716e168bdca499847cb2c6842de) just explicit and simple. With other languages you’ve have to glue languages together or maybe resort to some kind of horrible polymorphic kludge.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 24, 2020, 3:52am UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038/13 "2020-05-24T03:52:19Z")

</div>

Right, of course, there are many things you can’t do in those languages that you can do in Julia because of the type system. But the point I’m trying to make is that we tried very hard when designing the language not to force types on people. It’s a perfectly valid modality of using Julia to forget (or not know) about types. I think sometimes newcomers may feel like they _have_ to learn this whole type system all at once in order to use the language, but you very much do not. Don’t worry about it until you need it and then you can learn it quite incrementally—just learn what you need as you go along. You don’t have to jump into writing type heavy code right away. If you’re not defining your own types or doing low level bit twiddling, you can write a lot of useful Julia code that doesn’t mention types at all.

---

<div class="post-metadata">

**Author:** ![lhp22](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lhp22](https://discourse.julialang.org/u/lhp22)\
**Post date:** [May 27, 2020, 11:07pm UTC](https://discourse.julialang.org/t/solved-uint-is-not-int/40038/14 "2020-05-27T23:07:35Z")

</div>

I didn’t want to cause so debate. I’ve worked a lot with OCaml, and its static typing is now in my mind, because that detects so many errors. And the first language I’ve known was C. So, wanting to write types for all my variables is an habit. Don’t take this as personal for the Julia’s creator and maintainer : Julia’s type system is awesome and really usefull, thanks 🙂
