rafaqz / Interfaces.jl

Macros to define and implement interfaces, to ensure they are checked and correct.
MIT License
72 stars 4 forks source link

Interface inheritance: use abstract types rather than symbols for optional components? #33

Open rafaqz opened 8 months ago

rafaqz commented 8 months ago

Each optional component would (hidden in the macro) define the abstract type like:

abstract type SomeInterface_option <: SomeInterface end

We could make it nicer looking, but that's the direct translation from what we have now

First we can just inherit the main interface

@interface NewInterface <: BaseInterface NewObject conditions 

But we could also inherit the optional component, which itself inherits the interface:

@interface NewInterface <: BaseInterface_opional_thing NewObject conditions 

We would test the interface by iterating over supertype until we hit AbstractInterface then working backwards, so the basal interface is tested first. When @@implements has multiple options, we would take the union of the interfaces to test at each step to avoid duplicating any tests..

But... what we really want is a hack for multiple inheritance:

@interface NewInterface <: Union{BaseInterface_option,SomeInterface} NewType conditions

Where we would drop the real inheritance and instead write something like:

abstract type NewInterface{T} <: Interface{T} end
inherits(::Type{<:NewInterface}) = (BaseInterface_option, SomeInterface)

Then all tests and trait queries would recurse over inherits(T) if a trait is false for T.

gdalle commented 8 months ago

I agree that inheriting interfaces is a key ingredient (see our recent Discourse discussion). Note that it is already possible at the moment, simply by adding Interfaces.test of the parent interface as a mandatory condition. So the key question is what we hope to achieve with a different syntax / implementation.

rafaqz commented 8 months ago

Yes its possible in the testing, but you don't get any traits to check that you passed it. The idea is that both tests and traits would be inherited automatically.

rafaqz commented 7 months ago

@gdalle I just realised having inheritance for options means we have to define the NamedTuple of options inside the macro so we can get the keys to define abstract types for them. So your changes allowing us to move objects out of the macro wont be able to be used.

gdalle commented 7 months ago

I'm kind of keen on that change, because hypothetically large interfaces could even span several code files

rafaqz commented 7 months ago

Sure, I guessed as much.

But: how do we get the NamedTuple keys into the macro as an expression. We cant inspect a variable in a macro and we cant define types later at runtime.

(You may need to choose nice layout or iheritance... or just move individual option functions to files and make all your options single lines calling those functions)

gdalle commented 7 months ago

If there's a choice I definitely go for inheritance.