Zero boilerplate
Loading the package registers structural contracts and behavioral invariants for the core Base abstract types. No declarations to write yourself.
The standard library's abstract types, already wired up with TypeContracts. Load the package and Vector, Dict, Set, String and Number carry their protocols.
BaseTypeContracts.jl is to TypeContracts.jl what BaseInterfaces.jl is to Interfaces.jl: a collection of pre-written contracts for Julia's Base abstract types. Load it, and the standard-library protocols are registered for you.
using TypeContracts, BaseTypeContracts
implements(Vector{Int}, AbstractArray) # true
implements(Dict{String,Int}, AbstractDict) # true
implements(Int, Number) # true
# Boolean check for all applicable Base contracts at once:
all_implements(Vector{Int}) # true
# Full diagnostic result:
BaseTypeContracts.check(Vector{Int})
# Dict(AbstractArray => (satisfied = true, …))The contracts live apart from TypeContracts so the core stays dependency-free and unopinionated about Base. You opt in by adding BaseTypeContracts — and you get a worked, tested reference for how non-trivial contracts (parametric types, the iteration protocol, operator methods) are written.
| Contract target | Protocol | Example satisfying type |
|---|---|---|
AbstractArray | indexing | Vector{Int} |
AbstractDict | associative | Dict{String,Int} |
AbstractSet | set | Set{Int} |
AbstractString | string | String |
Number | arithmetic | Int, Float64 |
Real | ordering | Float64, Int64 |
AbstractFloat | IEEE 754 laws | Float64, Float32 |
Integer | bitwise ops | Int64, UInt8 |
AbstractChar | code point | Char |
IO | byte I/O | IOBuffer |
AbstractChannel | messaging | Channel{Int} |
Iterable | iteration | queried explicitly |
See The Base Contracts for the exact method and invariant list behind each one.