Hello,
I am executing the following function, and it should only accept Int64 as the argument. However, I find that it does not throw an error when any other data types like float is used. Can anyone pls clarify? C is a dataframe with two columns
function num_lang(year::Int64)
** boo=C[:,:year].==year**
** return(count(boo))**
end
Probably you are in a running Julia session where you previously defined a version of num_lang
that didn’t have the type restriction, and it’s still in memory, so the old version is being called instead of num_lang(::Int)
. Restart Julia and it should work as expected.
PS. Quote your code (and don’t post screenshots): Please read: make it easier to help you
3 Likes
I agree with the answer of stevengj… most likely you had already defined a more broad version of num_lang
.
If you try on a new Julia session you will see that the version you posted “accepts” only integer parameters, throwing a MethodError
otherwise:
julia> function num_lang(year::Int64)
println("I am in num_lang(::Int64)")
end
num_lang (generic function with 1 method)
julia> num_lang("one")
ERROR: MethodError: no method matching num_lang(::String)
Closest candidates are:
num_lang(::Int64)
@ Main REPL[1]:1
Stacktrace:
[1] top-level scope
@ REPL[2]:1
julia> num_lang(1)
I am in num_lang(::Int64)
1 Like