Skip to content

[ENHANCEMENT] Support broadcast calls in @interface #3

@mtfishman

Description

@mtfishman

Support broadcast calls in @interface, for example, @interface SparseArrayInterface 2 .* a and @interface SparseArrayInterface b .= 2 .* a.

I believe those lower to Broadcast.materialize(Broadcast.broadcasted(*, 2, a)) and Broadcast.materialize!(b, Broadcast.broadcasted(*, 2, a)) respectively, so those would need @interface overloads.

Broadcast.materialize(bc::Broadcast.Broadcasted) is defined as Base.copy(instantiate(bc)), and the implementation of Base.copy(::Broadcast.Broadcasted) gets a bit tricky because of the logic around how to instantiate the output when it can't easily be inferred from the inputs (https://github.com/JuliaLang/julia/blob/v1.11.2/base/broadcast.jl#L888-L915). But maybe we can assume types are inferrable/concrete for the time being to simplify things, in which case it can just be something like:

Base.copy(bc::Broadcast.Broadcasted)
  T = combine_eltypes(bc.f, bc.args)
  dest = similar(bc, T)
  copyto!(dest, bc)
  return dest
end

Broadcast.materialize!(dest, bc::Broadcast.Broadcasted) basically just ends up as Base.copyto!(dest, bc::Broadcast.Broadcasted).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions