BinaryTraits.jl
BinaryTraits.jl copied to clipboard
Contradictory Traits Break When Applied to the Type Hierarchy
I would like to apply a default trait to an abstract type, and then special case some subtypes with the negated trait. Take for example below:
abstract type Bar end
struct Foo <: Bar end
@trait A_Foo prefix Is,Not
@implement Is{A_Foo} by getfoo(_)
@assign Bar with Not{A_Foo}
@assign Foo with Is{A_Foo}
@traitfn getfoo(::Not{A_Foo}) = "Not a Foo!"
# This passes, but seems like it shouldn't
@check Foo
Foo passes the check, but getfoo(Foo()) fails because I haven't implemented getfoo(::Foo) yet in accordance with the Is{A_Foo} interface. This behavior seems nuanced since it requires associating only one of Is{A_Foo} or Not{A_Foo} to each type at a time, so it may contradict BinaryTraits's implementation.
Alternatively, if there's a better way/a design pattern to achieve this behavior (default trait applied to abstract type, special cased implementation of the negated trait to subtypes) I am open implementing that instead
I can take a shot at this in a day or two, but a potential resolution, at least for the checker, looks like it would be to sort the pairs by depth of the types in the tree. This way, we start with higher (more abstract) types and proceed down the tree to the concrete/leaf types.
Then we can resolve contradictory traits by either the latest trait assigned to the type itself, or the last supertype which had the trait assigned to it
Relevant lines: https://github.com/tk3369/BinaryTraits.jl/blob/master/src/assignment.jl#L12-L16
@SBuercklin I think this package is dead. No commits > 3 years and no response here in a year. It is breaking on julia 1.10. We are going to move away from it. What are you using these days?
I opened an issue for the break on 1.10 on this repo, it's related to a bug in 1.10.0 and there's a stopgap fix in that issue
As for replacements I've continued to use this. If I were to try something different it would likely be RequiredInterfaces.jl, but I've not tested it out yet.
Currently, I don't have the bandwidth to do any substantial work in this repo. Happy to review contribution or answer any question, however. There are other trait packages around so you could also check them out. I am not familiar with the maturity and potential tradeoffs, however.