tk3369 / BinaryTraits.jl

Can do or not? It's easy. See https://tk3369.github.io/BinaryTraits.jl/dev/
MIT License
53 stars 3 forks source link

Contradictory Traits Break When Applied to the Type Hierarchy #60

Open SBuercklin opened 1 year ago

SBuercklin commented 1 year ago

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

SBuercklin commented 1 year ago

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

kylebeggs commented 10 months ago

@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?

SBuercklin commented 10 months ago

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.

tk3369 commented 3 months ago

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.