Broadcasting and single characters

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)

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

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.

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).

1 Like

What about iterating over a single characters codepoints?

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}:

or directly access bytes via:

julia> reinterpret(UInt32, '∀')

Ah, yes of course!

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

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

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



Perfect! Thanks, Bogumił!