# Just curious about a code in Base

**URL:** <https://discourse.julialang.org/t/just-curious-about-a-code-in-base/64893>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 19, 2021, 2:04am UTC](https://discourse.julialang.org/t/just-curious-about-a-code-in-base/64893 "2021-07-19T02:04:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [July 19, 2021, 2:04am UTC](https://discourse.julialang.org/t/just-curious-about-a-code-in-base/64893/1 "2021-07-19T02:04:47Z")

</div>

Hi, I was hanging around `Base` and I found this method.

```julia
@edit Base.hash_32_32(UInt32(1)) # juila 1.6.1

function hash_32_32(n::UInt32)
    a::UInt32 = n # what is it the purpose of this line?
    a = a + 0x7ed55d16 + a << 12
    a = a ⊻ 0xc761c23c ⊻ a >> 19
    a = a + 0x165667b1 + a << 5
    a = a + 0xd3a2646c ⊻ a << 9
    a = a + 0xfd7046c5 + a << 3
    a = a ⊻ 0xb55a4f09 ⊻ a >> 16
    return a
end

```

I have no idea of the purpose of the first line of the method!  
Why is it necessary the type assertion?

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [July 19, 2021, 2:35am UTC](https://discourse.julialang.org/t/just-curious-about-a-code-in-base/64893/2 "2021-07-19T02:35:59Z")

</div>

The first line probably exists to avoid mutating `n`. If you got rid of that line, it would be customary to call the function `hash_32_32!` to make it clear that it returns the answer in place (EDIT: This is nonsense, sorry!). If you are only curious about the type assertion, I agree it seems redundant.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [July 19, 2021, 2:45am UTC](https://discourse.julialang.org/t/just-curious-about-a-code-in-base/64893/3 "2021-07-19T02:45:31Z")

</div>

> [@josePereiro](#):
>
> ```julia
> function hash_32_32(n::UInt32)
> a::UInt32 = n # what is it the purpose of this line?
> a = a + 0x7ed55d16 + a << 12
> a = a ⊻ 0xc761c23c ⊻ a >> 19
> a = a + 0x165667b1 + a << 5
> a = a + 0xd3a2646c ⊻ a << 9
> a = a + 0xfd7046c5 + a << 3
> a = a ⊻ 0xb55a4f09 ⊻ a >> 16
> return a
> end
> 
> ```

That is not right I think. The value of the variable `n` is immutable. It is not necessary to work with a copy.

---

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [July 19, 2021, 2:45am UTC](https://discourse.julialang.org/t/just-curious-about-a-code-in-base/64893/4 "2021-07-19T02:45:43Z")

</div>

Thanks, I’m actually more curious now! What do you mean with mutating a `UInt32`?

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [July 19, 2021, 2:45am UTC](https://discourse.julialang.org/t/just-curious-about-a-code-in-base/64893/5 "2021-07-19T02:45:45Z")

</div>

Operations on numbers (like `UInt32`) are never “in place” (since they’re immutable) so the function wouldn’t need the `!`. The reason for the line `a::UInt32` is that, in that context, the type annotation is a “guarantee” that `a` will always be a `UInt32`, no matter what happens to it. In practice, this means that every `a = a...` operation is followed by a `convert(UInt32, a)`.

See:

```julia
julia> function foo(x::Int)
           y::Int = x
           y += 1.0
           y
       end
foo (generic function with 1 method)

julia> function bar(x::Int)
           y = x
           y += 1.0
           y
       end
bar (generic function with 1 method)

julia> foo(1)
2

julia> bar(1)
2.0

```

* * *

edit with another example, with a number that cannot be converted back to Int

```julia
julia> function baz(x)
           y::Int = x
           y += 1.1
           y
       end
baz (generic function with 1 method)

julia> baz(1)
ERROR: InexactError: Int64(2.1)
Stacktrace:
 [1] Int64 at ./float.jl:710 [inlined]
 [2] convert at ./number.jl:7 [inlined]
 [3] baz(::Int64) at ./REPL[39]:3
 [4] top-level scope at REPL[40]:1

```

---

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [July 19, 2021, 2:47am UTC](https://discourse.julialang.org/t/just-curious-about-a-code-in-base/64893/6 "2021-07-19T02:47:46Z")

</div>

Thanks, I didn’t know it!

---

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [July 19, 2021, 2:49am UTC](https://discourse.julialang.org/t/just-curious-about-a-code-in-base/64893/7 "2021-07-19T02:49:52Z")

</div>

Those this feature of the type assertions have a name? It it for changing the title to a more informative one!

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [July 19, 2021, 3:16am UTC](https://discourse.julialang.org/t/just-curious-about-a-code-in-base/64893/8 "2021-07-19T03:16:04Z")

</div>

It doesn’t seem to have its own name. See the second paragraph of this section for more info [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#Type-Declarations)
