Compile-time enforcement
@verify and @verify_all run during precompilation. A module fails to load if any concrete subtype is missing a mandatory method or has the wrong inferred return type.
Catch missing methods and wrong return types at precompilation time — before your code runs.
TypeContracts.jl brings Go-style structural interface contracts to Julia. You declare which methods every concrete subtype of an abstract type must implement, annotate expected return types, and have violations caught at precompilation time — before your code ever runs.
using TypeContracts
abstract type AbstractShape end
function area end
function perimeter end
@contract AbstractShape begin
area(::Self) :: Float64
perimeter(::Self) :: Float64
end
struct Circle <: AbstractShape
radius::Float64
end
area(c::Circle)::Float64 = π * c.radius^2
perimeter(c::Circle)::Float64 = 2π * c.radius
@verify Circle # passes at precompile time
struct Square <: AbstractShape
side::Float64
end
area(s::Square) = s.side^2 # perimeter missing
@verify Square # InterfaceError: module fails to loadTypeContracts starts from structure: declare what methods must exist and what they must return, then enforce it. Behavioral testing (@invariants / test_behavior) is an addition on top of the structural foundation — the precompile-time guarantee is the anchor.
@contract / @verify → methods exist, return types correct (precompile time)
@invariants → methods behave correctly (test time)