Skip to content

Commit e07a9a4

Browse files
authored
Change behavior of eltype() (JuliaData#102)
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).
1 parent 0efe75a commit e07a9a4

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

src/value.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ leveltype(::Type{<:CategoricalValue{T}}) where {T} = T
1212
leveltype(::Type{<:CategoricalString}) = String
1313
leveltype(::Type) = throw(ArgumentError("Not a categorical value type"))
1414
leveltype(x::Any) = leveltype(typeof(x))
15-
# eltype() is a synonym of leveltype() for categorical values
16-
Base.eltype(::Type{T}) where {T <: CatValue} = leveltype(T)
17-
Base.eltype(x::CatValue) = eltype(typeof(x))
1815

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

152149
# AbstractString interface for CategoricalString
153150
Base.string(x::CategoricalString) = get(x)
151+
Base.eltype(x::CategoricalString) = Char
154152
Base.length(x::CategoricalString) = length(get(x))
155153
Base.endof(x::CategoricalString) = endof(get(x))
156154
Base.sizeof(x::CategoricalString) = sizeof(get(x))

test/01_typedef.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ using CategoricalArrays: DefaultRefType, level, reftype, leveltype, catvalue, i
5252

5353
@test iscatvalue(x)
5454
@test iscatvalue(typeof(x))
55-
@test eltype(x) === String
56-
@test eltype(typeof(x)) === String
55+
@test eltype(x) === Char
56+
@test eltype(typeof(x)) === Char
5757
@test leveltype(x) === String
5858
@test leveltype(typeof(x)) === String
5959
@test reftype(x) === DefaultRefType

test/05_convert.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ using CategoricalArrays: DefaultRefType, level, reftype, leveltype, catvalue, is
1717
v3 = catvalue(3, pool)
1818
@test iscatvalue(v1)
1919
@test iscatvalue(typeof(v1))
20-
@test eltype(v1) === Int
21-
@test eltype(typeof(v1)) === Int
20+
@test eltype(v1) === Any
21+
@test eltype(typeof(v1)) === Any
2222
@test leveltype(v1) === Int
2323
@test leveltype(typeof(v1)) === Int
2424
@test reftype(v1) === DefaultRefType

test/08_string.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ using CategoricalArrays
3030
@test isempty(v1)
3131
@test !isempty(v2)
3232

33+
@test eltype(v1) === Char
34+
@test eltype(v2) === Char
35+
3336
@test length(v1) === 0
3437
@test length(v2) === 4
3538

0 commit comments

Comments
 (0)