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
as
clause is provided, thencategory
(an abstract type) will be used as the super type of the trait type. - If the
prefix
clause is provided, then it allows the user to choose different prefixes than the default ones (Can
andCannot
) e.g.prefix Is,Not
orprefix Has,Not
. - If the
with
clause 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
trait
function 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 using
BinaryTraits`.
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
— ConstantAssignable
Assignable
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}
orCannot{Fly}
func
: function that must be implemented to satisfy this traitargs
: argument types of the functionfunc
kwargs
: keyword argument names of the functionfunc
ret
: return type of the functionfunc
BinaryTraits.InterfaceReview
— TypeInterfaceReview
An InterfaceReview object contains the validation results of an interface.
Fields
data_type
: the type being checkedresult
: true if the type fully implements all required contractsimplemented
: an array of implemented contractsmisses
: an array of unimplemented contracts