# Broadcasting on Vector{SubString{String}} does not work in Julia 1.7.0 rc1?

**URL:** https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913
**Category:** General Usage
**Tags:** strings, broadcasting
**Created:** [October 16, 2021, 8:38pm UTC](https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913 "2021-10-16T20:38:36Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![pszufe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pszufe/32/23452_2.png) [@pszufe](https://discourse.julialang.org/u/pszufe)
#### Post date: [October 16, 2021, 8:38pm UTC](https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913/1 "2021-10-16T20:38:36Z")

</div>

I use Julia 1.7.0rc1 and it seems that broadcasting works for `Vector{String}` and does not work on `Vector{SubString{String}}`. Is this something intended or I run into a bug?

For `Vector{String}` all works as expected:

```julia
julia> length.(["1","22","333"])
3-element Vector{Int64}:
 1
 2
 3

```

For `Vector{SubString{String}}` Comprehension works:

```julia
julia> [length(a) for a in split.("1 22 333")]
3-element Vector{Int64}:
 1
 2
 3

```

However broadcasting does not:

```julia
julia> length.(split.("1 22 333"))
3

julia> parse.(Int,split.("1 22 333"))
ERROR: MethodError: no method matching parse(::Type{Int64}, ::Vector{SubString{String}})

```

Debuggin details:

```julia
julia> versioninfo()
Julia Version 1.7.0-rc1
Commit 9eade6195e (2021-09-12 06:45 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake)

```

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 16, 2021, 8:45pm UTC](https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913/2 "2021-10-16T20:45:55Z")

</div>

Very weird edge case, might be a bug.

Note though that you don’t need the `.` for split - `Strings` are treated as scalars for broadcasting purposes:

```julia
julia> parse.(Int, split("1 22 333"))
3-element Vector{Int64}:
   1
  22
 333

```

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 16, 2021, 8:47pm UTC](https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913/3 "2021-10-16T20:47:32Z")

</div>

It also only seems to happen with loop fusion:

```julia
julia> t = split.(SubString("1 22 333"))
3-element Vector{SubString{String}}:
 "1"
 "22"
 "333"

julia> parse.(Int, t)
3-element Vector{Int64}:
   1
  22
 333

```

Yeah, seems like a bug to me - please open an issue at [Issues · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues)! Thank you for finding this.

---

<div class="post-metadata">

### Author: ![pszufe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pszufe/32/23452_2.png) [@pszufe](https://discourse.julialang.org/u/pszufe)
#### Post date: [October 16, 2021, 9:00pm UTC](https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913/4 "2021-10-16T21:00:35Z")

</div>

Yes, however:

```julia
julia> split("1 22 333") == split.("1 22 333")
true

```

Yet the outcome of `split.("1 22 333")` mysteriously fails on broadcasting while to should not. Scalars in broadcasting should be treated as zero sized arrays, no?

---

<div class="post-metadata">

### Author: ![pszufe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pszufe/32/23452_2.png) [@pszufe](https://discourse.julialang.org/u/pszufe)
#### Post date: [October 16, 2021, 9:01pm UTC](https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913/5 "2021-10-16T21:01:48Z")

</div>

Sukera it fails when broadcasting was made on a scalar. See my answer to other post.

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [October 16, 2021, 9:13pm UTC](https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913/6 "2021-10-16T21:13:28Z")

</div>

Following the Julia Manual

> Moreover, like all vectorized “dot calls,” these “dot operators” are _fusing_ . For example, if you compute `2 .* A.^2 .+ sin.(A)` (or equivalently `@. 2A^2 + sin(A)` , using the [`@.`](https://docs.julialang.org/en/v1/base/arrays/#Base.Broadcast.@ __dot__ ) macro) for an array `A` , it performs a _single_ loop over `A` , computing `2a^2 + sin(a)` for each element of `A` .

Let us try to translate it for our case. You have `"1 22 333"` input. You iterate it. Strings have special handling in broadcasting so it is a single element loop (i.e. we do not iterate string), so it seems that just `parse(Int, split("1 22 333"))` should be done as you passed one element in the input. And this fails.

Am I missing something here?

---

<div class="post-metadata">

### Author: ![pszufe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pszufe/32/23452_2.png) [@pszufe](https://discourse.julialang.org/u/pszufe)
#### Post date: [October 16, 2021, 9:22pm UTC](https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913/7 "2021-10-16T21:22:20Z")

</div>

Here are the outputs:

```julia
julia> parse.(Int, split("1 22 333"))
3-element Vector{Int64}:
   1
  22
 333

julia> parse.(Int, split.("1 22 333"))
ERROR: MethodError: no method matching parse(::Type{Int64}, ::Vector{SubString{String}})

```

It looks like the second parse gets the entire `Vector` for some reason? Which is not the expected behavior.

BTW `@.` fails in a similar way:

```julia
julia> @. parse(Int, split("1 22 333"))
ERROR: MethodError: no method matching parse(::Type{Int64}, ::Vector{SubString{String}})

```

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [October 16, 2021, 9:30pm UTC](https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913/8 "2021-10-16T21:30:41Z")

</div>

What I say is that this is the expected behavior. Since you input a 0-dimensional argument then `parse` must be called only once on the output of the `split` (since 0-dimensional input contains exactly one element).

For example what would you expect to happen if you did `parse.(Int, split.(Ref("1 22 333")))`?

Note that for broadcasting `"1 22 333"` and `Ref("1 22 333")` is exactly the same since:

```julia
julia> Base.broadcastable("1 22 33")
Base.RefValue{String}("1 22 33")

```

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [October 16, 2021, 9:42pm UTC](https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913/9 "2021-10-16T21:42:54Z")

</div>

Do you find this expected?

```julia
julia> string.(identity.(Ref([1,2,3])))
"[1, 2, 3]"

```

---

<div class="post-metadata">

### Author: ![pszufe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pszufe/32/23452_2.png) [@pszufe](https://discourse.julialang.org/u/pszufe)
#### Post date: [October 16, 2021, 9:52pm UTC](https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913/10 "2021-10-16T21:52:12Z")

</div>

so I understand that whenever more than one dot operator is involved, the dimension for loop fusion is taken from whatever is executed as first?

---

<div class="post-metadata">

### Author: ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)
#### Post date: [October 16, 2021, 10:05pm UTC](https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913/11 "2021-10-16T22:05:14Z")

</div>

It’s fused. There is only one iteration. The string is treated as a scalar for broadcasting, and it is the only thing being iterated over. So there is no choice to do something different.

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [October 16, 2021, 10:07pm UTC](https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913/12 "2021-10-16T22:07:22Z")

</div>

Yes if you pass only one argument and just do function call nesting.

In general no, consider:

```julia
julia> .+([1 2], .^(2, [3, 4]))
2×2 Matrix{Int64}:
  9 10
 17 18

```

So all three arguments take part in determination of the dimension.

However, the dimensions of the output can be determined in a way independent from the functions involved. Only dimensionality of their arguments matters.

Here a special case is if you pass only 0-dimensional arguments, when - for convenience - the result of the operation is not wrapped in a 0-dimensional container, but is unwrapped when broadcasting is performed. And this is probably the reason of the confusion.

See e.g.

```julia
julia> sin.(fill(1))
0.8414709848078965

```

and you can see that 0-dimensional array was dropped.

This behavior is relevant if you wanted to implement custom broadcasting for your own types, as 0-dimensional containers require special treatment.

---

<div class="post-metadata">

### Author: ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)
#### Post date: [October 16, 2021, 10:27pm UTC](https://discourse.julialang.org/t/broadcasting-on-vector-substring-string-does-not-work-in-julia-1-7-0-rc1/69913/13 "2021-10-16T22:27:15Z")

</div>

It might be helpful to compare this

```julia
parse.([Int, Float64], split.("1 2"))

```

which fails, with this

```julia
parse.([Int, Float64], Ref(["1", "2"]))

```

which fails for essentially the same reason.  
The length of the first argument (for iteration) is 2, and the length of the second argument is 1, so it is reused.
