Skip to content

Commit

Permalink
Make @interface overloads take types, not instances (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Dec 7, 2024
1 parent 3455bb2 commit 54bfc9d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ struct SparseArrayInterface end
Define interface functions.

````julia
@interface SparseArrayInterface() function Base.getindex(a, I::Int...)
@interface SparseArrayInterface function Base.getindex(a, I::Int...)
checkbounds(a, I...)
!isstored(a, I...) && return getunstoredindex(a, I...)
return getstoredindex(a, I...)
end
@interface SparseArrayInterface() function Base.setindex!(a, value, I::Int...)
@interface SparseArrayInterface function Base.setindex!(a, value, I::Int...)
checkbounds(a, I...)
iszero(value) && return a
if !isstored(a, I...)
Expand Down
4 changes: 2 additions & 2 deletions examples/README.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ using Test: @test
struct SparseArrayInterface end

# Define interface functions.
@interface SparseArrayInterface() function Base.getindex(a, I::Int...)
@interface SparseArrayInterface function Base.getindex(a, I::Int...)
checkbounds(a, I...)
!isstored(a, I...) && return getunstoredindex(a, I...)
return getstoredindex(a, I...)
end
@interface SparseArrayInterface() function Base.setindex!(a, value, I::Int...)
@interface SparseArrayInterface function Base.setindex!(a, value, I::Int...)
checkbounds(a, I...)
iszero(value) && return a
if !isstored(a, I...)
Expand Down
12 changes: 6 additions & 6 deletions src/interface_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ or:
```
to:
```julia
Derive.call(::typeof(SparseArrayInterface()), Base.getindex, a, I...)
Derive.call(SparseArrayInterface(), Base.getindex, a, I...)
```
=#
function interface_call(interface::Union{Symbol,Expr}, func::Expr)
Expand All @@ -48,7 +48,7 @@ Rewrite:
```
to:
```julia
Derive.call(::typeof(SparseArrayInterface()), Base.getindex, a, I...)
Derive.call(SparseArrayInterface(), Base.getindex, a, I...)
```
=#
function interface_ref(interface::Union{Symbol,Expr}, func::Expr)
Expand All @@ -65,7 +65,7 @@ Rewrite:
```
to:
```julia
Derive.call(::typeof(SparseArrayInterface()), Base.setindex!, a, value, I...)
Derive.call(SparseArrayInterface(), Base.setindex!, a, value, I...)
```
=#
function interface_setref(interface::Union{Symbol,Expr}, func::Expr)
Expand All @@ -78,14 +78,14 @@ end
#=
Rewrite:
```julia
@interface SparseArrayInterface() function Base.getindex(a, I::Int...)
@interface SparseArrayInterface function Base.getindex(a, I::Int...)
!isstored(a, I...) && return getunstoredindex(a, I...)
return getstoredindex(a, I...)
end
```
to:
```julia
function Derive.call(::typeof(SparseArrayInterface()), Base.getindex, a, I::Int...)
function Derive.call(::SparseArrayInterface, Base.getindex, a, I::Int...)
!isstored(a, I...) && return getunstoredindex(a, I...)
return getstoredindex(a, I...)
end
Expand All @@ -98,7 +98,7 @@ function interface_definition(interface::Union{Symbol,Expr}, func::Expr)
# We use `Core.Typeof` here because `name` can either be a function or type,
# and `typeof(T::Type)` outputs things like `DataType`, `UnionAll`, etc.
# while `Core.Typeof(T::Type)` returns `Type{T}`.
new_args = [:(::typeof($interface)); :(::Core.Typeof($name)); args]
new_args = [:(::$interface); :(::Core.Typeof($name)); args]
return globalref_derive(
codegen_ast(
JLFunction(; name=new_name, args=new_args, kwargs, rettype, whereparams, body)
Expand Down

0 comments on commit 54bfc9d

Please sign in to comment.