Skip to content

StackOverflowError in scalartype #12

Description

@lkdvos

Currently, the fallback definition of scalartype(::Type) does not seem to function as intended:

function scalartype(T::Type)
    @warn _warn_message(scalartype, T) maxlog = 1
    if applicable(eltype, T)
        return scalartype(eltype(T))
    else
        throw(ArgumentError(_error_message(scalartype, T)))
    end
end

In particular, there is a definition in Base: eltype(::Type) = Any, which ensures that eltype is always applicable, and thus scalartype(eltype(T)) leads to infinite recursion when T = Any. This does not usually show up as a problem, however it means that for new types, the cryptic StackOverflowError appears instead of an actually helpful errormessage.

julia> using VectorInterface

julia> struct Test end

julia> scalartype(Test())
┌ Warning: The function `scalartype` is not implemented for (values of) type `Test`;
│ this fallback will disappear in future versions of VectorInterface.jl
└ @ VectorInterface ~/.julia/packages/VectorInterface/TAlcJ/src/fallbacks.jl:20
ERROR: StackOverflowError:

I can think of a couple solutions:

  1. We define scalartype(::Type{Any}) = Any, which attempts to still make everything work without figuring out scalartypes.
  2. We define scalartype(::Type{Any}) = throw(MethodError(scalartype, Type{Any})), which will then show the backtrace so you can figure out where the scalartype inference went wrong
  3. We combine both by implementing option 1 and adding a warning message.

I would want either 1 or 2, which depends on the question of do we really want to support vectors with Any or not. I feel like explicitly throwing an error is definitely a bit restrictive, so I'd rather go for option 1.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions