I would like to announce BinaryTraits.jl, a new traits package that focuses on usability and interface specification. You can find documentation from the project repo as usual. In addition, I have provided some examples for the standard Iteration/Indexing and AbstractArray interfaces in the examples folder.
Why another package?
Julia’s traits story has been slowly developing over the past several years. There is nothing official in the language but several people attempted to experiment new ideas about how to make that work. Meanwhile, the Holy Traits pattern is mostly used given that it requires no additional package dependency.
However, I am not personally not satisfied because implementing Holy Traits requires quite a bit of code. SimpleTraits.jl does look very nice but I’m quite intimidated by its syntax/design. So, I end up developing a package that I think it should be easy to use and provide additional functionalities such as formally specifying interface contracts and validating those contracts.
What can it do?
- Define traits and assigning them to your own data types
- Define composite traits that exhibits all of the underlying traits
- Define interface contracts for a trait
- Check if your data type fully implements all interface contracts
Try it out!
This package is still in its early stage. I have been messing with the API several times in the past week. I would be pleased to receive any feedback and ideas for improving this package. You can either reply to this post or just submit an issue to my github repo.
The package is being registered at the moment. It will take 3 days due to the mandatory waiting period. But, I am so excited that I am sharing with you about the news now!
Update 2020-04-20 (version 0.2.0)
I just tagged a new version after making some significant code changes and taking several very generous PRs from @klacru Most improvements relate to the interface contracts specification and verification:
Interface contracts can now be specified with keyword arguments & complex argument types. Duck typed arguments are supported as well.
Interface contracts are now propagated to sub-traits for composite traits. Let’s say an abstract type
CanFlytrait, which requires a
flymethod per interface contract. Then all subtypes of
Birdsare required to implement the same contract.
P.S. More ideas and upcoming enhancements are logged in GitHub.
Update 2020-05-04 (version 0.3.0)
We had some breaking changes but they’re worth 100%.
@implementmacro now accepts an underscore argument, which indicates where the object argument should be passed for that function.
Traits defined from one module can now be referenced/used from another module. This would be useful for framework providers to define traits that implementations should follow.
See updated documentation for details.
Update 2020-05-17 (version 0.4.0)
This release implements a new parametric type design to represent traits. The reason for the change is that the previous design feels too “magical” in the sense that custom types are defined with specific prefixes/suffixes. The new design feels more natural and Julian.
Update 2020-06-07 (version 0.5.0)
This release adds return type check for interface contracts. Return type of an interface contract is covariant i.e. the implementation must return an object of a type that is a subtype of the required return type as specified in the contract. See updated documentation about variance for more details.