Skip to content

Using parametric struct and only defining the top level type makes it errors out #51

@minecraft2048

Description

@minecraft2048

I'm trying to use SumTypes to replicate Rust Result type, and I expected the Result type to be able to carry all types. Here's the example code:

using SumTypes

#Rust like result type
@sum_type Result{A, B} begin
    Ok{A}(::A)
    Err{B}(::B)
end

#Some default errors
struct SequenceError{T}
    prev::T
    cur::T
end

struct Good 
    good::Int
end


function test(a) :: Result{Good, SequenceError}
    if a
        return Ok(Good(1))
    else
        return Err(SequenceError(1,1))
    end
end

Somehow if my test function tries to return SequenceError it errors out with cannot convert error:

julia> test(true)
Ok(Good(1))::Result{Good, SequenceError}

julia> test(false)
ERROR: MethodError: Cannot `convert` an object of type 
  Result{Uninit,SequenceError{Int64}} to an object of type 
  Result{Good,SequenceError}

Closest candidates are:
  convert(::Type{<:Result{A, B}}, ::Result{Uninit, B}) where {A, B}
   @ Main ~/.julia/packages/SumTypes/Mrn3L/src/sum_type.jl:197
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:84
  convert(::Type{Result{A, B}}, ::Result{Uninit, B}) where {A, B}
   @ Main ~/.julia/packages/SumTypes/Mrn3L/src/sum_type.jl:196
  ...

Stacktrace:
 [1] test(a::Bool)
   @ Main ~/workdir/nanoavionics/result.jl:24
 [2] top-level scope
   @ REPL[59]:1

But if I fully specify the type then it works:

julia> function test2(a) :: Result{Good, SequenceError{Int}}
           if a
               return Ok(Good(1))
           else
               return Err(SequenceError(1,1))
           end
       end
test2 (generic function with 1 method)

julia> test2(true)
Ok(Good(1))::Result{Good, SequenceError{Int64}}

julia> test2(false)
Err(SequenceError{Int64}(1, 1))::Result{Good, SequenceError{Int64}}

This is maybe #44, but I'm not that familiar with Julia type system yet

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions