# \`Char\` and \`broadcastable\`

**URL:** https://discourse.julialang.org/t/char-and-broadcastable/61221
**Category:** Internals & Design
**Created:** [May 15, 2021, 8:06pm UTC](https://discourse.julialang.org/t/char-and-broadcastable/61221 "2021-05-15T20:06:07Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [May 15, 2021, 8:06pm UTC](https://discourse.julialang.org/t/char-and-broadcastable/61221/1 "2021-05-15T20:06:07Z")

</div>

A related question: Is it intentional that `broadcastable` is not explicitly defined for `Char` in `broadcast.jl`? Instead the default method for iterables is used. `String` in contrast is treated as a scalar, as discussed above.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 15, 2021, 8:33pm UTC](https://discourse.julialang.org/t/char-and-broadcastable/61221/2 "2021-05-15T20:33:42Z")

</div>

> [@matthias314](#):
>
> A related question: Is it intentional that `broadcastable` is not explicitly defined for `Char` in `broadcast.jl` ? Instead the default method for iterables is used. `String` in contrast is treated as a scalar, as discussed above.

It doesn’t need to be — like `Number`, a `Char` can act as a 0-dimensional array. (IIRC it used to be that `Char` was a subtype of `Integer` so it got its behavior then, and kept it when it was split off into its own type.)

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [May 15, 2021, 10:26pm UTC](https://discourse.julialang.org/t/char-and-broadcastable/61221/3 "2021-05-15T22:26:39Z")

</div>

I’m sorry, I don’t fully understand. The current definitions include `broadcastable(x::Number) = x` and `broadcastable(x::Char) = collect(x)`. It seems to me that `Char` is the only plain bits type for which `broadcastable` calls `collect`. (This once threw me off when implementing broadcasting for a custom type.) Do you think this is correct or a bug?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 16, 2021, 1:00am UTC](https://discourse.julialang.org/t/char-and-broadcastable/61221/4 "2021-05-16T01:00:40Z")

</div>

> [@matthias314](#):
>
> `broadcastable(x::Char) = collect(x)` . It seems to me that `Char` is the only plain bits type for which `broadcastable` calls `collect` . (This once threw me off when implementing broadcasting for a custom type.) Do you think this is correct or a bug?

Sorry, I must have misremembered — it must be that `Char` is missing some method after all that would allow it to work with `broadcast` without the `collect`? I’m not sure what it is, since `Char` supports `size` and `getindex` and `axes` similar to `Number`. What happens if you remove that method and try to `broadcast`?

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [May 16, 2021, 1:25am UTC](https://discourse.julialang.org/t/char-and-broadcastable/61221/5 "2021-05-16T01:25:27Z")

</div>

The code

```julia
import Base.Broadcast: broadcastable
broadcastable(x::Char) = x
['a','b'] .* 'd'

```

results in

```julia
ERROR: MethodError: no method matching getindex(::Char, ::CartesianIndex{0})

```

So it appears to be `getindex` that doesn’t work.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 16, 2021, 2:43am UTC](https://discourse.julialang.org/t/char-and-broadcastable/61221/6 "2021-05-16T02:43:45Z")

</div>

> [@matthias314](#):
>
> So it appears to be `getindex` that doesn’t work.

Seems like a bug (a missing method), since a zero-argument getindex works for Char.

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [May 16, 2021, 4:09pm UTC](https://discourse.julialang.org/t/char-and-broadcastable/61221/7 "2021-05-16T16:09:32Z")

</div>

> [@stevengj](#):
>
> Seems like a bug (a missing method)

You’re right. If you create such a method for `getindex`, then it works:

```julia
import Base: getindex
getindex(x::Char, ::CartesianIndex{0}) = x

import Base.Broadcast: broadcastable
broadcastable(x::Char) = x

['a','b'] .* 'c' # works

```

Is it worth filing this on GitHub?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 16, 2021, 5:09pm UTC](https://discourse.julialang.org/t/char-and-broadcastable/61221/8 "2021-05-16T17:09:56Z")

</div>

> [@matthias314](#):
>
> Is it worth filing this on GitHub?

Seems like it. (If nothing else, it would give a cleaner way to broadcast using `Char`, without allocating a temporary array with `collect`.)

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [May 19, 2021, 2:43pm UTC](https://discourse.julialang.org/t/char-and-broadcastable/61221/9 "2021-05-19T14:43:31Z")

</div>

I’ve created a [PR](https://github.com/JuliaLang/julia/pull/40877).
