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

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

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

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

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

  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

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

Functions

BinaryTraits.traits — Function
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.

source
BinaryTraits.init_traits — Function
init_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.

source

Types

BinaryTraits.Assignable — Constant
Assignable

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

source
BinaryTraits.Contract — Type
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.

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
source
BinaryTraits.InterfaceReview — Type
InterfaceReview

An 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
source