This page contains the most important macros, functions, and types that you should be aware of.


@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, then category (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 and Cannot) e.g. prefix Is,Not or prefix 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.

@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}.

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

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.

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

  1. A dispatch function that uses trait function to determine the type of trait args
  2. An implementation function that contains trait type arguments in the front of the signature


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


traits(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.

required_contracts(module::Module, T::Assignable)

Return a set of contracts that is required to be implemented for the provided type T.


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.



Assignable represents any data type that can be associated with traits. It essentially covers all data types including parametric types e.g. AbstractArray

Contract{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.


  • 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

An InterfaceReview object contains the validation results of an interface.


  • 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