Function seems to return an incorrect data type

The following function seems to work well because the value it returns is of type Int8

function abc()
    #local x::Int8  # in a local declaration
    x::Int8 = 10   # as the left-hand side of an assignment
    x
end

x = abc()

println(x)
print(typeof(x))
10
Int8

However, if I remove the x inside the abc() function, the result changes, and now it is of type Int64.

function abc()
    #local x::Int8  # in a local declaration
    x::Int8 = 10   # as the left-hand side of an assignment
end

x = abc()

println(x)
print(typeof(x))
10
Int64

In my opinion, this makes no sense, because in both cases, the variable “x” is returned, and it should always be of type Int8.

I suppose I am missing something, but I do not know what it is. Any hints?

1 Like

The value of an assignment expression is the right hand side, so in the second case x is not returned but the right hand side 10, which is an Int.

13 Likes

Fyi, this is documented in Julia manual’s FAQ: What is the return value of an assignment?

8 Likes