Skip to content
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

@sum_type doesn't work with constrained type parameter in variant #73

Closed
Tortar opened this issue Feb 10, 2024 · 7 comments · Fixed by #78
Closed

@sum_type doesn't work with constrained type parameter in variant #73

Tortar opened this issue Feb 10, 2024 · 7 comments · Fixed by #78

Comments

@Tortar
Copy link
Contributor

Tortar commented Feb 10, 2024

e.g. this

julia> using SumTypes

julia> @sum_type A{X<:Union{Real, SumTypes.Uninit}} begin
           B{X<:Union{Int, SumTypes.Uninit}}(a::X)
           C{X}(b::X)
       end

throws

ERROR: LoadError: constructor parameters (Any[:(X <: Union{Int, SumTypes.Uninit})]) for B, not a subset of sum type parameters [:X]
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] generate_constructor_data(T_name::Symbol, T_params::Vector{Symbol}, T_params_constrained::Vector{Any}, T_nameparam::Expr, hide_variants::Bool, blk::Expr)
   @ SumTypes ~/.julia/dev/SumTypes/src/sum_type.jl:75
 [3] _sum_type(T::Expr, hidden::QuoteNode, blk::Expr)
   @ SumTypes ~/.julia/dev/SumTypes/src/sum_type.jl:31
 [4] _sum_type(T::Expr, blk::Expr)
   @ SumTypes ~/.julia/dev/SumTypes/src/sum_type.jl:6
 [5] var"@sum_type"(__source__::LineNumberNode, __module__::Module, T::Any, args::Vararg{Any})
   @ SumTypes ~/.julia/dev/SumTypes/src/sum_type.jl:3
in expression starting at REPL[6]:1

but I think this could work

@Tortar Tortar changed the title @sum_type doesn't work with constrained type parameter @sum_type doesn't work with constrained type parameter Feb 10, 2024
@Tortar
Copy link
Contributor Author

Tortar commented Feb 10, 2024

inspecting the test I understood I should use Union{Real, Uninit}, this feels a bit awkward but probably then this can be closed

@Tortar Tortar closed this as completed Feb 10, 2024
@Tortar
Copy link
Contributor Author

Tortar commented Feb 10, 2024

ok I actually changed slightly the issue, this is now something which could be probably improved

@Tortar Tortar reopened this Feb 10, 2024
@Tortar Tortar changed the title @sum_type doesn't work with constrained type parameter @sum_type doesn't work with constrained type parameter in variant Feb 10, 2024
@MasonProtter
Copy link
Owner

Yeah, this'll require a bit of thought to design correctly but should be possible I think. I wonder if we should just insert the Union{..., Uninit} ourselves if people constrain the parameters?

@Tortar
Copy link
Contributor Author

Tortar commented Feb 13, 2024

In my opinion yes, I'm already doing this in MixedStrucTypes.jl, there shouldn't be any problem doing that if I'm not mistaken

@MasonProtter
Copy link
Owner

I must say though, I'm a little skeptical of the idea of there being instances of type A for which B is not constructable. Like, is there usecases where that's desirable?

@Tortar
Copy link
Contributor Author

Tortar commented Feb 15, 2024

I'm not sure, but maybe it can be considered analogous to the case where you have a Union of types, there you can constrain parameters differently if you want

@Tortar
Copy link
Contributor Author

Tortar commented May 20, 2024

I ended up having ambiguity problems in MixedStructTypes because of this issue so I fixed it :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants