-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StackOverflowError in scalartype
#12
Comments
I would pick 2, as having |
While this is true, that also holds for |
I found a bug in another package that is caused by this stack overflow. I was about to open an issue here when I found this one. See iitis/QuantumInformation.jl#105 |
This should be fixed as soon as these changes are tagged. That being said, @araujoms , this fix only means that you will get a MethodError instead of a StackOverflow, I think your linked issue requires to implement this interface for the JuMP variable system, as I think these do not subtype Number (I might be wrong about this, I have no experience with JuMP myself) |
No, I just tested it with the current git, I still get a stack overflow. JuMP variables in fact do not subtype Number, but they implement a method for |
Does this resolve the issue? |
Well it gives my an |
This is intended behaviour, because it will otherwise just fail further down the line, all methods in VectorInterface are by default only defined for scalartypes that subtype Number, so even if we were to just add a fallback, it still will break: using VectorInterface
import JuMP
VectorInterface.scalartype(t::Type{<:JuMP.VariableRef}) = t
model = JuMP.Model()
JuMP.@variable(model, rho[1:4, 1:4] in JuMP.PSDCone()) julia> scale(rho, one(scalartype(rho))) # expect this to work
ERROR: MethodError: no method matching scale(::LinearAlgebra.Symmetric{JuMP.VariableRef, Matrix{JuMP.VariableRef}}, ::JuMP.AffExpr) Because again, all of our methods assume that the scalar type subtypes number. This being said, if you need any help getting a VectorInterface implementation set up, I can definitely help you out and give some pointers. |
Thanks for the offer, but the error is not in my package, I just needed a function that it provided. Also, that package seems abandoned, so I don't think its devs will be interested. |
In principle I would say this is a problem at the level of JuMP, which defines the eltypes but does not implement VectorInterface. There could be a very straightforward package extension somewhere for these things to be compatible, so if someone really needs this it can definitely be done. |
I really need the function, but I'll just use the version from OpenQuantumBase instead, which also didn't work but I managed to fix: USCqserver/OpenQuantumBase.jl#109. I just thought it would be nice to get the version from QuantumInformation working as well, but it's more trouble than it's worth. |
Currently, the fallback definition of
scalartype(::Type)
does not seem to function as intended:In particular, there is a definition in Base:
eltype(::Type) = Any
, which ensures thateltype
is always applicable, and thusscalartype(eltype(T))
leads to infinite recursion whenT = 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.I can think of a couple solutions:
scalartype(::Type{Any}) = Any
, which attempts to still make everything work without figuring out scalartypes.scalartype(::Type{Any}) = throw(MethodError(scalartype, Type{Any}))
, which will then show the backtrace so you can figure out where the scalartype inference went wrongI 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.The text was updated successfully, but these errors were encountered: