I encountered some unexpected behavior regarding broadcasted functions for strings.
Let’s say we have two strings:
a = "abc"
b = "abx"
When checking for equality, the result was as I expected:
a == b
# result: false
When I wanted to check for the equality for each individual glyph using a broadcasted or dotted call, I was expecting to get a boolean vector giving the equality for each glyph.
That was not the case, however:
a .== b
# result: false
# expected: [true, true, false]
When doing the same for arrays, the result is (close to) what I would expect:
x = [1, 2, 3]
y = [1, 2, 4]
x == y
# result: false
x .== y
# result: 3-element BitVector:
# 1
# 1
# 0
I ended up using this workaround which gave me the desired result:
split(a, "") .== split(b, "")
# result: 3-element BitVector:
# 1
# 1
# 0
I know that in Julia strings aren’t arrays of ASCII chars, but instead are more complex due to Unicode encodings. However, indexing into strings does mirror the array indexing interface, giving individual glyphs as a result, even though they may correspond to multiple bytes behind the scene.
My questions are:
What is the reason for strings behaving this way for dotted (equality) calls?
Wouldn’t it be advantageous if == and .== did behave the same for strings as they do for arrays?
julia version 1.6.1