Skip to content

API Reference

BaseTypeContracts is intentionally small: loading it registers the Base contracts, and a couple of helpers let you query them. The full checking, testing and introspection toolkit lives in TypeContracts.

Module

BaseTypeContracts.BaseTypeContracts Module
julia
BaseTypeContracts

Ready-made TypeContracts contracts for Julia Base types — analogous to BaseInterfaces.jl for Interfaces.jl.

Loading this package registers structural contracts and behavioral invariants for the core Base abstract types so you don't have to write them yourself:

Contract targetProtocol
AbstractArrayindexing
AbstractDictassociative
AbstractSetset
AbstractStringstring
Numberarithmetic
Realordering
AbstractFloatIEEE 754 laws
Integerbitwise ops
AbstractCharcode point
IObyte I/O
AbstractChannelmessaging
AbstractRangestepped sequence
Base.AbstractLockmutual exclusion
Logging.AbstractLoggerlog sink
AbstractDisplaydisplay sink
Base.AbstractPatterntext matching
Exceptionerror reporting (invariants only)
Random.AbstractRNGrandom streams (invariants only)
Iterable (marker)iteration

Access pattern

Every TypeContracts tool works directly against a concrete instantiation like Vector{Int} — the registry key is the bare AbstractArray UnionAll, and TypeContracts resolves a parameterized concrete type back to it automatically:

julia
using TypeContracts, BaseTypeContracts

implements(Vector{Int}, AbstractArray)         # true
implements(Dict{String,Int}, AbstractDict)     # true
implements(Vector{Int}, Iterable)              # iteration marker

@verify Vector{Int} for_contract=AbstractArray # seals verified_trait(AbstractArray, Vector{Int})

check/all_implements are convenience wrappers scoped to exactly the curated Base types this package covers: instead of naming one contract at a time, check(T) returns every applicable one in a single Dict, and all_implements(T) summarizes that dict as a Bool:

julia
BaseTypeContracts.check(Vector{Int})           # Dict(AbstractArray => (satisfied=true, ...))
BaseTypeContracts.all_implements(Vector{Int})  # true if all applicable contracts pass

The iteration marker

BaseTypeContracts.Iterable Type
julia
Iterable

Marker type carrying the Base iteration contract. No type is <: Iterable; query it explicitly with satisfies(T, Iterable) or test_behavior(T, Iterable, objs).

Helpers

BaseTypeContracts.check Function
julia
check(T::Type) -> Dict{Type, NamedTuple}

Run satisfies(T, B) for every registered Base contract B where T <: B. Returns a dict mapping each applicable Base type to its satisfies result.

Scoped to exactly the curated Base types this package covers (base_contract_types): a convenience for getting every applicable contract's result in one call, rather than naming each one individually with satisfies/implements.

For a simple boolean result use all_implements, or for a single interface use implements(T, AbstractArray) etc.

BaseTypeContracts.all_implements Function
julia
all_implements(T::Type) -> Bool

Return true if T satisfies every applicable Base contract. Equivalent to all(r -> r.satisfied, values(check(T))). Designed for direct use with @test:

julia
@test all_implements(Vector{Int})
@test all_implements(Dict{String, Int})

Returns true (vacuously) for a type with no applicable Base contract at all — unlike TypeContracts' own implements(T), which throws ArgumentError in that case. A typo'd or unrelated type therefore "passes" silently; if that matters, check !isempty(check(T)) first, or use implements(T) for the throwing variant.

BaseTypeContracts.base_contract_types Function
julia
base_contract_types() -> Tuple

Return the Base abstract types for which contracts are registered (excludes the Iterable marker).