Unexpected method error during function call

Why does the following code cause a method error on adding type nothing with an Int?

function flipname(nm)
    i = findfirst(isequal(','), nm) # returns type nothing if finds no occurrence
    i == nothing ? nm : nm[i+2:length(nm)] * " " * nm[1:i-1]

Meanwhile, the following code behaves as expected:

n = nothing
n == nothing ? println("nothing") : n + 1

How do you call the function to provoke the error?
Because for me:

julia> flipname("a, b")
"b a"
julia> flipname("ab")
julia> flipname("a,b")
" a"

it works somehow.

weird, had been running in jupyter notebook and


returned the method error.

Restarted the kernel and back to expected output somehow

There was probably another flipname() method which you had defined previously in that same Jupyter kernel session. For example, you might have had a flipname(nm::AbstractString) or flipname(nm::String), so defining another more general method flipname(nm) would not replace that old method but would happily live alongside it.

This sort of thing is much easier for us to help with if you can post the full error output, including the stack trace, rather than just saying that the code causes an error.

1 Like

That’s exactly the issue, thx.

Will do on posting full error msgs in the future.