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

Add parse method #392

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Commits on Apr 29, 2022

  1. Add parse method

    Motivation:
    
    ```
    julia> using CategoricalArrays
    
    julia> x = categorical(["2", "1"], ordered=true)
    2-element CategoricalArray{String,1,UInt32}:
     "2"
     "1"
    
    julia> parse(Int64, x[1])
    ERROR: MethodError: no method matching parse(::Type{Int64}, ::CategoricalValue{String, UInt32})
    Closest candidates are:
      parse(::Type{T}, ::AbstractChar; base) where T<:Integer at C:\Users\Dennis Bal\.julia\juliaup\julia-1.7.1+0~x64\share\julia\base\parse.jl:40
      parse(::Type{T}, ::AbstractString; base) where T<:Integer at C:\Users\Dennis Bal\.julia\juliaup\julia-1.7.1+0~x64\share\julia\base\parse.jl:240
      parse(::Type{T}, ::AbstractString; kwargs...) where T<:Real at C:\Users\Dennis Bal\.julia\juliaup\julia-1.7.1+0~x64\share\julia\base\parse.jl:379
    Stacktrace:
     [1] top-level scope
       @ REPL[176]:1
    
    julia> parse.(Int64, x)
    ERROR: MethodError: no method matching parse(::Type{Int64}, ::CategoricalValue{String, UInt32})
    Closest candidates are:
      parse(::Type{T}, ::AbstractChar; base) where T<:Integer at C:\Users\Dennis Bal\.julia\juliaup\julia-1.7.1+0~x64\share\julia\base\parse.jl:40
      parse(::Type{T}, ::AbstractString; base) where T<:Integer at C:\Users\Dennis Bal\.julia\juliaup\julia-1.7.1+0~x64\share\julia\base\parse.jl:240
      parse(::Type{T}, ::AbstractString; kwargs...) where T<:Real at C:\Users\Dennis Bal\.julia\juliaup\julia-1.7.1+0~x64\share\julia\base\parse.jl:379
    Stacktrace:
     [1] _broadcast_getindex_evalf
       @ .\broadcast.jl:670 [inlined]
     [2] _broadcast_getindex
       @ .\broadcast.jl:653 [inlined]
     [3] getindex
       @ .\broadcast.jl:597 [inlined]
     [4] copy
       @ .\broadcast.jl:899 [inlined]
     [5] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(parse), Tuple{Base.RefValue{Type{Int64}}, CategoricalVector{String, UInt32, String, CategoricalValue{String, UInt32}, Union{}}}})
       @ Base.Broadcast .\broadcast.jl:860
     [6] top-level scope
       @ REPL[177]:1
    
    julia> import Base.parse
    
    julia> parse(T::DataType, catval::CategoricalValue) = parse(T, unwrap(catval))
    parse (generic function with 17 methods)
    
    julia> parse(Int64, x[1])
    2
    
    julia> parse.(Int64, x)
    2-element Vector{Int64}:
     2
     1
    ```
    
    I am not sure if I added the code in the correct place, or if it makes more sense somewhere else. I also don't know if `Base.parse` would need to be explicitly imported. I just tried to do what has been done is the surrounding lines.
    KronosTheLate authored Apr 29, 2022
    Configuration menu
    Copy the full SHA
    b79df4d View commit details
    Browse the repository at this point in the history