Skip to content

BaseTypeContracts.jlReady-made contracts for Base

The standard library's abstract types, already wired up with TypeContracts. Load the package and Vector, Dict, Set, String and Number carry their protocols.

What is BaseTypeContracts.jl? ​

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.

julia
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, …))

Why a separate package? ​

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.

Registered contracts ​

Contract targetProtocolExample satisfying type
AbstractArrayindexingVector{Int}
AbstractDictassociativeDict{String,Int}
AbstractSetsetSet{Int}
AbstractStringstringString
NumberarithmeticInt, Float64
RealorderingFloat64, Int64
AbstractFloatIEEE 754 lawsFloat64, Float32
Integerbitwise opsInt64, UInt8
AbstractCharcode pointChar
IObyte I/OIOBuffer
AbstractChannelmessagingChannel{Int}
Iterableiterationqueried explicitly

See The Base Contracts for the exact method and invariant list behind each one.