Behavior of broadcasted chop

question

#1

I do not understand the following behavior:

julia> x = "ab"
"ab"

julia> dump(chop(x))
SubString{String}
  string: String "ab"
  offset: Int64 0
  ncodeunits: Int64 1

julia> dump(chop.([x]))
Array{SubString{String}}((1,))
  1: SubString{String}
    string: String "a"
    offset: Int64 0
    ncodeunits: Int64 1

Why does broadcasting of chop change string field of SubString?


#2

This is master, not v0.6.2, right?


#3

Right. Under 0.6.2 it works OK. That is why I have put it under Development tag. To be precise I use:

Julia Version 0.7.0-DEV.3354
Commit 9b5eed2b6c* (2018-01-09 08:03 UTC)

#4

This is weird, as chop apparently gets the "ab". Can reproduce with map too.

julia> VERSION
v"0.7.0-DEV.3385"

julia> x = "ab";

julia> justdump(x) = (print("called with "); dump(x); x);

julia> dump(map(chop ∘ justdump, [x]))
called with String "ab"
Array{SubString{String}}((1,))
  1: SubString{String}
    string: String "a"
    offset: Int64 0
    ncodeunits: Int64 1

julia> dump(map(chop, [x]))
Array{SubString{String}}((1,))
  1: SubString{String}
    string: String "a"
    offset: Int64 0
    ncodeunits: Int64 1

#5

I have found the offending operation:

convert(::Type{SubString{S}}, s::AbstractString) where {S<:AbstractString} =
    SubString(convert(S, s))

I think defining additionally

convert(::T, s::T) where {T<:SubString} = s

should fix the problem.


#6

PR https://github.com/JuliaLang/julia/pull/25525