This page contains the most important macros, functions, and types that you should be aware of.
Macros
BinaryTraits.@trait — Macro@trait <name> [as <category>] [prefix <positive>,<negative>] [with <trait1,trait2,...>]Create a new trait type for name called $(name)Trait:
- If the asclause is provided, thencategory(an abstract type) will be used as the super type of the trait type.
- If the prefixclause is provided, then it allows the user to choose different prefixes than the default ones (CanandCannot) e.g.prefix Is,Notorprefix Has,Not.
- If the withclause is provided, then it defines a composite trait from existing traits.
Note that you must specify at least 2 traits to make a composite trait.
BinaryTraits.@assign — Macro@assign <T> with <trait1, trait2, ...>Assign traits to the data type T.  The traits may be positive or negative e.g. Can{Fly} or Cannot{Swim}.
BinaryTraits.@implement — Macro@implement <trait> by <sig>Register a new interface contract as specified by sig for the specified trait.
You can use the @check function to verify your implementation after these interface contracts are registered.  The signature may use an underscore to indicate a placeholder for the data type that exihibits the trait.  Return type is currently optional and unchecked.
BinaryTraits.@check — Macro@check(T::Assignable)
check(module::Module, T::Assignable)Check if the data type T has fully implemented all trait functions that it was previously assigned. See also: @assign.
BinaryTraits.@traitfn — Macro@traitfn <function definition>Define a function that contains traits arguments. The macro expands the function definition using the Holy Traits pattern such that two functions are defined:
- A dispatch function that uses traitfunction to determine the type of trait args
- An implementation function that contains trait type arguments in the front of the signature
Example
@traitfn second(v::Is{Indexable}) = v[2]is expanded to:
second(v::T) where T = second(BinaryTraits.trait(Indexable, T), v)
second(::Positive{Indexable}, v::T) where T = v[2]Functions
BinaryTraits.traits — Functiontraits(m::Module, T::Assignable)Returns a set of traits that the data type T exhibits, including the ones that were assigned to any supertypes of T. See also @assign.
BinaryTraits.is_trait — Functionis_trait(x)Return true if x is a trait type.
BinaryTraits.required_contracts — Functionrequired_contracts(module::Module, T::Assignable)Return a set of contracts that is required to be implemented for the provided type T.
BinaryTraits.init_traits — Functioninit_traits(module::Module)This function should be called like init_traits(@__MODULE__) inside the __init__()' method of each module usingBinaryTraits`.
Alternatively it can be called outside the module this way: using Module; init_traits(Module), if Module missed to call it within its __init__ function.
This is required only if the traits/interfaces are expected to be shared across modules.
BinaryTraits.set_verbose! — Functionset_verbose!(flag::Bool)For debugging - set flag to print macro expansions
Types
BinaryTraits.Assignable — ConstantAssignableAssignable represents any data type that can be associated with traits. It essentially covers all data types including parametric types e.g. AbstractArray
BinaryTraits.Contract — TypeContract{T <: DataType, F <: Function, N}A contract refers to a function defintion func that is required to satisfy a trait. The function func must accepts args and returns ret.
Fields
- trait: a trait type e.g.- Can{Fly}or- Cannot{Fly}
- func: function that must be implemented to satisfy this trait
- args: argument types of the function- func
- kwargs: keyword argument names of the function- func
- ret: return type of the function- func
BinaryTraits.InterfaceReview — TypeInterfaceReviewAn InterfaceReview object contains the validation results of an interface.
Fields
- data_type: the type being checked
- result: true if the type fully implements all required contracts
- implemented: an array of implemented contracts
- misses: an array of unimplemented contracts