Search code examples
migrationjuliamethod-signature

Function not matching parameters in language version migration, and how to know the signature of a function


I'm migrating some code I have from an old version of Julia to Julia 1.0.0. Although I already adapted the basic (type -> mutable struct, immutable -> struct), I'm getting the error

ERROR: LoadError: MethodError: no method matching ... 

The not matching function is in a function returned by a function (that is, I use a functor that change the behavior of the function it returns by changing the parameters of the inner function by closure). The problem is in the returned function (in case this is influencing in the problem, I don't know).

The questions are:

  1. Is this common in migrating code? What is happening?
  2. Is there a way to print the signature of a function in order to debug the problem? (I tried tipeof(f) but did not return the needed information).
  3. Do you know a practical way to debug these signature not matching problems?

Thanks in advance.


Solution

  • Ad 1. It might happen when the type of what you pass to the function changed and function signature is restrictive. For example in Julia 0.6 you have the following return type of transpose:

    julia> transpose([1,2,3])
    1×3 RowVector{Int64,Array{Int64,1}}:
     1  2  3
    

    and in Julia 1.0

    julia> transpose([1,2,3])
    1×3 LinearAlgebra.Transpose{Int64,Array{Int64,1}}:
     1  2  3
    

    and then if your function expected RowVector it will fail.

    Ad 2. You can do it like this:

    julia> f(x::Int) = y::Int -> y + x
    f (generic function with 1 method)
    
    julia> a = f(10)
    #11 (generic function with 1 method)
    
    julia> methods(a)
    # 1 method for generic function "#11":
    [1] (::getfield(Main, Symbol("##11#12")))(y::Int64) in Main at REPL[5]:1
    

    Ad 3. It is recommended to use Julia 0.7 for code migration, because it prints warnings on things that changed between Julia 0.6 and Julia 1.0.