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

Error when creating SumType in an untyped vector #66

Closed
Tortar opened this issue Jan 21, 2024 · 2 comments · Fixed by #75
Closed

Error when creating SumType in an untyped vector #66

Tortar opened this issue Jan 21, 2024 · 2 comments · Fixed by #75

Comments

@Tortar
Copy link
Contributor

Tortar commented Jan 21, 2024

Let's say you have

using SumTypes

@sum_type A{X,Y} begin
    B{X}(x::X)
    C{Y}(y::Y)
end

it seems to work fine

julia> B(1)
B(1)::A{Int64, Uninit}

julia> [B(i) for i in 1:2]
2-element Vector{A{Int64, Uninit}}:
 B(1)::A{Int64, Uninit}
 B(2)::A{Int64, Uninit}

but instead this errors

julia> [B(1)]
ERROR: MethodError: convert(::Type{A{Int64, Uninit}}, ::A{Int64, Uninit}) is ambiguous.

Candidates:
  convert(::Type{var"##_T#226"}, x::var"##_T#226") where {X, Y, var"##_T#226"<:A{X, Y}}
    @ Main ~/.julia/dev/SumTypes/src/sum_type.jl:212
  convert(::Type{<:A{X, Y}}, x::A{X, Uninit}) where {X, Y}
    @ Main ~/.julia/dev/SumTypes/src/sum_type.jl:203
  convert(::Type{A{X, Y}}, x::A{X, Uninit}) where {X, Y}
    @ Main ~/.julia/dev/SumTypes/src/sum_type.jl:202

Possible fix, define
  convert(::Type{A{X, Uninit}}, ::A{X, Uninit}) where X

Stacktrace:
 [1] __inbounds_setindex!(A::Vector{A{Int64, Uninit}}, x::A{Int64, Uninit}, i1::Int64)
   @ Base ./array.jl:1026
 [2] vect(X::A{Int64, Uninit})
   @ Base ./array.jl:165
 [3] top-level scope
   @ REPL[17]:1

Instead if you type the vector it works:

julia> A[B(1)]
1-element Vector{A}:
 B(1)::A{Int64, Uninit}

julia> A{Int}[B(1)]
1-element Vector{A{Int64}}:
 B(1)::A{Int64, Uninit}
@MasonProtter
Copy link
Owner

Ah crap. There's a lot of convert methods spat out by @sum_type, trying to do various useful things. I'll have to hunt down a way to fix this ambiguity. :(

@Tortar
Copy link
Contributor Author

Tortar commented Jan 25, 2024

Seems related JuliaDynamics/LightSumTypes.jl#15

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