Why does adding types to my Julia function cause a MethodError?

Viewed 44

I'm migrating from Python and am pretty new to Julia, but this one is proving pretty difficult to figure out. I'm hoping to later compile packages via PackageCompiler for later use in other languages, so I need to able to export C callable methods. If I define the following code:

module MyModule

export testmethod1
export testmethod2

function testmethod1(x::Cfloat, y::Cfloat)::Cfloat
    return x^y*4
end

function testmethod2(x, y)
    return x^y*4
end

end

And call println(MyModule.testmethod2(2.3, 3.2)), I get 57.489570831681995

However, when I call println(MyModule.testmethod1(2.3, 3.2)), I get

ERROR: LoadError: MethodError: no method matching testmethod1(::Float64, ::Float64)

But when I type check these variables, say, println(typeof(2.3)), I get Float64! Why is Julia unable to match the input I am giving to the function when I specify Cfloat as the data type?

1 Answers

Cfloat is a Float32 2.3 is a Float64. Note that you probably shouln't be using Cfloat (or other types that are C+type) frequently. Those are aliases for C interop.

Related