Skip to content

Commit

Permalink
Change behavior of eltype()
Browse files Browse the repository at this point in the history
eltype(CategoricalString) should return Char for consistency with getindex.
eltype(CategoricalValue) should not be defined, which means it returns Any
(like any other type by default).
  • Loading branch information
nalimilan committed Nov 22, 2017
1 parent 0efe75a commit 526a934
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
4 changes: 1 addition & 3 deletions src/value.jl
Expand Up @@ -12,9 +12,6 @@ leveltype(::Type{<:CategoricalValue{T}}) where {T} = T
leveltype(::Type{<:CategoricalString}) = String
leveltype(::Type) = throw(ArgumentError("Not a categorical value type"))
leveltype(x::Any) = leveltype(typeof(x))
# eltype() is a synonym of leveltype() for categorical values
Base.eltype(::Type{T}) where {T <: CatValue} = leveltype(T)
Base.eltype(x::CatValue) = eltype(typeof(x))

# integer type of category reference codes used by categorical value
reftype(::Type{<:CatValue{R}}) where {R} = R
Expand Down Expand Up @@ -151,6 +148,7 @@ end

# AbstractString interface for CategoricalString
Base.string(x::CategoricalString) = get(x)
Base.eltype(x::CategoricalString) = Char
Base.length(x::CategoricalString) = length(get(x))
Base.endof(x::CategoricalString) = endof(get(x))
Base.sizeof(x::CategoricalString) = sizeof(get(x))
Expand Down
4 changes: 2 additions & 2 deletions test/01_typedef.jl
Expand Up @@ -52,8 +52,8 @@ using CategoricalArrays: DefaultRefType, level, reftype, leveltype, catvalue, i

@test iscatvalue(x)
@test iscatvalue(typeof(x))
@test eltype(x) === String
@test eltype(typeof(x)) === String
@test eltype(x) === Char
@test eltype(typeof(x)) === Char
@test leveltype(x) === String
@test leveltype(typeof(x)) === String
@test reftype(x) === DefaultRefType
Expand Down
4 changes: 2 additions & 2 deletions test/05_convert.jl
Expand Up @@ -17,8 +17,8 @@ using CategoricalArrays: DefaultRefType, level, reftype, leveltype, catvalue, is
v3 = catvalue(3, pool)
@test iscatvalue(v1)
@test iscatvalue(typeof(v1))
@test eltype(v1) === Int
@test eltype(typeof(v1)) === Int
@test eltype(v1) === Any
@test eltype(typeof(v1)) === Any
@test leveltype(v1) === Int
@test leveltype(typeof(v1)) === Int
@test reftype(v1) === DefaultRefType
Expand Down
3 changes: 3 additions & 0 deletions test/08_string.jl
Expand Up @@ -30,6 +30,9 @@ using CategoricalArrays
@test isempty(v1)
@test !isempty(v2)

@test eltype(v1) === Char
@test eltype(v2) === Char

@test length(v1) === 0
@test length(v2) === 4

Expand Down

0 comments on commit 526a934

Please sign in to comment.