-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Require a method for specific subtypes only? #10
Comments
This can get complicated very quickly. Should |
In my case it shouldn't error, Having the possibility to specify optional methods would be nice indeed. Here is an extended example: using RequiredInterfaces
using Test
const RI = RequiredInterfaces
abstract type MyType{T} end
function my_func end
function my_other_func end
@required MyType begin
my_func(::MyType)
my_other_func(::MyType)
end
function my_feature(t::MyType)
return my_func(t)
end
function my_feature(t::MyType{<:AbstractVector})
return my_func(t) * " " * my_other_func(t)
end
struct MySubType <: MyType{Int} end
my_func(t::MySubType) = "Hello"
struct MyOtherSubType <: MyType{Vector{Int}} end
my_func(t::MyOtherSubType) = "Hello"
my_other_func(t::MyOtherSubType) = "world"
struct MyWrongOtherSubType <: MyType{Vector{Int}} end
my_func(t::MyWrongOtherSubType) = "Hello" The my_feature(MySubType()) # returns "Hello"
my_feature(MyOtherSubType()) # returns "Hello world"
my_feature(MyWrongOtherSubType()) # raises a not implemented error, and suggests to implement my_other_func However, @test RI.check_interface_implemented(MyType, MyOtherSubType) # passes
@test RI.check_interface_implemented(MyType, MyWrongOtherSubType) # fails
@test RI.check_interface_implemented(MyType, MySubType) # fails, and ideally should pass |
Yes, that is intentional - an interface cannot really have optional methods. In your example, I'd be open to add something like abstract type MyType{T} end
@required MyType begin
my_func(::MyType)
end
@required MyType{<:AbstractArray} begin
my_other_func(::MyType{<:AbstractArray})
end or, with some syntax sugar: @required T=MyType{<:AbstractArray} begin
my_other_func(::T)
end being equivalent to abstract type MyType end
# note how the UnionAll is resolved here by hand, and
# `MyTypeAbstractArray` inherits the interface of `MyType`
abstract type MyTypeAbstractArray <: MyType
@required MyType begin
my_func(::MyType)
end
@required MyTypeAbstractArray begin
my_other_func(::MyTypeAbstractArray)
end but I wouldn't make that happen automatically and/or add In other words, I'd say that no user of your package should ever expect or think of |
I didn't think of using a Thanks a lot for your help! |
Great! I'll leave this open, since support for |
I have a situation where I require a method only for some subtypes of the interface.
Would it be possible to support something like this for example?
The text was updated successfully, but these errors were encountered: