user12070812
user12070812

Reputation:

What does TypeError: in Type, in parameter, expected Type got xyz mean in Julia

I'm trying to work on some code and this is the error that I'm getting.

TypeError: in Type, in parameter, expected Type got xyz

Can anyone explain to me what would theoretically cause this to happen? This error message doesn't help me at all. Thanks!

Upvotes: 4

Views: 1657

Answers (2)

BoZenKhaa
BoZenKhaa

Reputation: 951

I was getting very similar error when incorrectly defining the return type of NamedTuples.

The correct way to do this is e.g.:

function fun(arg1::Int64, arg2::Float64)::NamedTuple{(:s1,:s2), Tuple{Int64, Float64}} 
    return (s1=arg1, s2=arg2) 
end

Also, it's useful to check the type of the instance you return to see the structure of the type signature, which you can then just copy-paste. E.g.

julia> typeof(fun(1, 2.))
NamedTuple{(:s1, :s2), Tuple{Int64, Float64}}

Upvotes: 0

One way to get this error is the following. Suppose you have defined a (non-trivial) type XYZ:

julia> struct XYZ
         x :: String
       end

and an instance of this type:

julia> xyz = XYZ("foo")
XYZ("foo")


You can get the type of object xyz (which is XYZ) using the typeof function:

julia> typeof(xyz)
XYZ


You can get the type of XYZ itself in the same way:

julia> typeof(XYZ)
DataType

however, this will return DataType for all types, which is not so useful when one wants to dispatch on the type of a type. For these cases, there exists the Type "singleton type". I think I have also seen it being referred to as a "pseudo-type", which I find easy to understand: everything works as if the type XYZ was itself of type Type{XYZ}:

julia> XYZ isa Type{XYZ}
true

julia> f(::Type{XYZ}) = 42
f (generic function with 1 method)

julia> f(XYZ)
42


However, there is one thing that you cannot do: build in this way the pseudo-type of an object that is not itself a type:

julia> Type{xyz}
ERROR: TypeError: in Type, in parameter, expected Type, got XYZ
Stacktrace:
 [1] top-level scope at REPL[3]:1


In conclusion, you probably used something like Type{xyz} on an object xyz, when what you really wanted was one of the following:

  • Type{XYZ} to refer to the (pseudo-)type of the type XYZ itself
  • typeof(xyz) to get the type of object xyz

Upvotes: 6

Related Questions