Broadcasting and single characters


#1

I just came across this surprising (to me) behaviour, that a single character is not considered as a scalar:

julia> myfun(a) = a
myfun (generic function with 1 method)

julia> myfun.(1)
1

julia> myfun.('a')
1-element Array{Char,1}:
 'a'

I would had expected that the last line just return a single ‘a’ and not [‘a’]. Is there a particular reason for the current behaviour ? I use julia 1.0.1.


#2

I would say that the following method is missing for Char:

Base.IteratorSize(::Type{Char}) = Base.HasShape{0}()

With this definition all works OK (and Char is treated as 0-dimensional array).


#3

What about iterating over a single characters codepoints?


#4

length of Char is defined as 1.

AFAIK to do this you currently have to write:

julia> codeunits(string('∀'))
3-element Base.CodeUnits{UInt8,String}:
 0xe2
 0x88
 0x80

or directly access bytes via:

julia> reinterpret(UInt32, '∀')
0xe2888000

#5

Ah, yes of course!


#6

What is the difference between myfun.('a') and myfun('a')?


#7

myfun('a') just returns 'a’ as expected.


#8

Thank you for your insights. Would you consider it as a bug? I am considering to file an issue.


#9

See https://github.com/JuliaLang/julia/pull/29819


#10

Perfect! Thanks, Bogumił!