Stream: helpdesk (published)

Topic: LoadError: too many parameters for type


view this post on Zulip Gaurav Wasnik (Feb 24 2021 at 05:13):

#CartesianGrid is defined like this
struct CartesianGrid{Dim,T} <: Mesh{Dim,T}
    #code
end

#This function throws error
function AbstractPlotting.plot!(grid::CartesianGrid{Dim,T}) where {Dim,T}
    #code
end

throws error LoadError: too many parameters for type
My understanding is that I'm specifying two parameters Dim and T whereas only one/none should be possible according to this error. Why is it so?

https://discourse.julialang.org/t/tricky-too-many-parameters-for-type-error/25182/6 this is also about the same error but I'm unable to understand the solution there.

view this post on Zulip Mason Protter (Feb 24 2021 at 05:17):

Could you give a runnable example that replciates the error you're getting?

view this post on Zulip Mason Protter (Feb 24 2021 at 05:18):

E.g. how is Mesh defined?

view this post on Zulip Mason Protter (Feb 24 2021 at 05:20):

Whatever is causing the error you're experiencing isn't actually caused by the code you've written here:

julia> abstract type Mesh{Dim, T} end

julia> struct CartesianGrid{Dim, T} <: Mesh{Dim, T} end

julia> f(::CartesianGrid{Dim, T}) where {Dim, T} = (Dim, T)
f (generic function with 1 method)

julia> f(CartesianGrid{2, Float64}())
(2, Float64)

view this post on Zulip Gaurav Wasnik (Feb 24 2021 at 05:30):

abstract type Mesh{Dim,T} <: Domain{Dim,T} end
abstract type Domain{Dim,T} end
#Code to reproduce the error
#CartesianGrid is defined in Meshes
using Meshes
using GLMakie

@recipe(CartesianGrid) do scene
    Attributes(
        seriescolor = :black,
    )
  end

function AbstractPlotting.plot!(grid::CartesianGrid{Dim,T}) where {Dim,T}
    #code
end

Thanks for the quick reply
Should I look into the source code of Meshes; and what could be the possible mistake leading to this error?

view this post on Zulip Mason Protter (Feb 24 2021 at 05:42):

It looks like the reason this is happening is that CartesianGrid isn't a struct, it's actually this:

julia> CartesianGrid
Combined{cartesiangrid, ArgType} where ArgType

It only has one type parameter, not two.

view this post on Zulip Mason Protter (Feb 24 2021 at 05:45):

Aha! it looks like it's the recipe that screwed things up:

julia> @macroexpand @recipe(CartesianGrid) do scene
           Attributes(
               seriescolor = :black,
           )
         end
quote
    #= /home/mason/.julia/packages/AbstractPlotting/cfp2m/src/recipes.jl:130 =#
    cartesiangrid() = begin
            #= /home/mason/.julia/packages/AbstractPlotting/cfp2m/src/recipes.jl:130 =#
            AbstractPlotting.not_implemented_for(cartesiangrid)
        end
    #= /home/mason/.julia/packages/AbstractPlotting/cfp2m/src/recipes.jl:131 =#
    const CartesianGrid{ArgType} = AbstractPlotting.Combined{cartesiangrid, ArgType}
[...]

See here, it defined a new CartesianGrid that shadowed Meshes.CartesianGrid in your module

view this post on Zulip Mason Protter (Feb 24 2021 at 05:45):

You could write

function AbstractPlotting.plot!(grid::Meshes.CartesianGrid{Dim,T}) where {Dim,T}
    #code
end

view this post on Zulip Gaurav Wasnik (Feb 24 2021 at 05:48):

Woah, thanks for the solution!
It works now and also the reason behind the error is clear! :+1:

view this post on Zulip Mason Protter (Feb 24 2021 at 05:49):

Glad I could help :grinning:

view this post on Zulip Kwaku Oskin (Feb 24 2021 at 07:39):

You can also ask this sort of questions in #meshes.jl channel. @Júlio Hoffimann is a main maintainer of the corresponding packages and usually very responsive.


Last updated: Nov 06 2024 at 04:40 UTC