# Bug when extending broadcast to ^ and non-explicit Int64

**URL:** https://discourse.julialang.org/t/bug-when-extending-broadcast-to-and-non-explicit-int64/123826
**Category:** Internals & Design
**Created:** [December 13, 2024, 11:06pm UTC](https://discourse.julialang.org/t/bug-when-extending-broadcast-to-and-non-explicit-int64/123826 "2024-12-13T23:06:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![croberts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/croberts/32/9465_2.png) [@croberts](https://discourse.julialang.org/u/croberts)
#### Post date: [December 13, 2024, 11:06pm UTC](https://discourse.julialang.org/t/bug-when-extending-broadcast-to-and-non-explicit-int64/123826/1 "2024-12-13T23:06:07Z")

</div>

Where should bugs be reported?

When extended to new type, broadcasting syntax fails in the final case below:

```julia
import Base.Broadcast.broadcasted  
  struct Bob end 
  broadcasted(f, ::Bob, t) = "here"
  Bob() .* 2
  Bob() .^ 2.
  Bob() .^ Int64(2)
  broadcast(^, Bob(), 2) 
  Bob() .^ 2 # breaks 

```

Strangely, using Int64(2) instead of 2 causes broadcast to work.

Is the bug in the parser? I am using Julia 1.11.2 on a Windows machine.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [December 13, 2024, 11:28pm UTC](https://discourse.julialang.org/t/bug-when-extending-broadcast-to-and-non-explicit-int64/123826/2 "2024-12-13T23:28:55Z")

</div>

The [broadcast interface](https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting) is involved and complicated, and it is admittedly challenging to implement fully. There will be a number of issues with an incomplete implementation such as yours. At first order, this fails because `^` with a literal lowers to `Base.literal_pow` with _three_ arguments, not two — see the fifth entry in the stacktrace here:

```julia
julia> Bob() .^ 2 # breaks
ERROR: MethodError: no method matching length(::Bob)

Closest candidates are:
  length(::ExponentialBackOff)
   @ Base error.jl:267
  length(::Core.SimpleVector)
   @ Base essentials.jl:770
  length(::BitSet)
   @ Base bitset.jl:348
  ...

Stacktrace:
 [1] _similar_shape(itr::Bob, ::Base.HasLength)
   @ Base ./array.jl:710
 [2] _collect(cont::UnitRange{Int64}, itr::Bob, ::Base.HasEltype, isz::Base.HasLength)
   @ Base ./array.jl:765
 [3] collect(itr::Bob)
   @ Base ./array.jl:759
 [4] broadcastable(x::Bob)
   @ Base.Broadcast ./broadcast.jl:743
 [5] broadcasted(f::typeof(Base.literal_pow), arg1::Function, arg2::Bob, args::Val{2})
   @ Base.Broadcast ./broadcast.jl:1345
 [6] top-level scope
   @ REPL[27]:1

```

That’s fairly straightforward to address, but there will likely be other challenges, not least of which are the number of ambiguities your definitions here incur.

---

<div class="post-metadata">

### Author: ![croberts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/croberts/32/9465_2.png) [@croberts](https://discourse.julialang.org/u/croberts)
#### Post date: [December 17, 2024, 5:09pm UTC](https://discourse.julialang.org/t/bug-when-extending-broadcast-to-and-non-explicit-int64/123826/3 "2024-12-17T17:09:36Z")

</div>

Thanks! Adding method

` broadcasted(f::typeof(Base.literal_pow), arg1::Function, iv::MyType, arg::Val{V}) where V = iv .^ typeof(V)(V)`

forced dispatch back to the three-argument broadcasted method and so solved my problem. I’m guessing the literal\_pow broadcast exist to enable lazy evaluation of certain things?
