Inherit.jl provides macros for inheriting fields and interface declarations
from a supertype. The macros generate native Julia types so you can work with
familiar syntax while ensuring that required methods are implemented. All
interface verification happens at compile time — there is no runtime hook,
no module __init__, and no penalty at dispatch.
@abstractbasedeclares an abstract supertype with inheritable fields and required method signatures.@implementdefines a concrete subtype that inherits its supertype's fields and is required to satisfy every inherited method declaration.@verify_interfacesis an optional, explicit compile-time gate that asserts every subtype defined in the current module satisfies its requirements.- Method declarations may use qualified names like
Base.closeto adopt an existing function from another module as part of the interface (new in v1.0). - Parametric types and constructor inheritance via
super()are supported.
using Inherit
@abstractbase struct Fruit
weight::Float64
function cost(fruit::Fruit, unitprice::Float64) end
end
@implement struct Apple <: Fruit
coresize::Int
end
function cost(apple::Apple, unitprice::Float64)
apple.weight * unitprice * (apple.coresize < 5 ? 2.0 : 1.0)
end
@verify_interfaces
println(cost(Apple(3.0, 4), 1.0))
# 6.0A qualified declaration adopts an existing foreign function:
@abstractbase mutable struct AbstractFile
path::String
function Base.close(f::AbstractFile) end
end
@implement mutable struct LogFile <: AbstractFile end
function Base.close(f::LogFile)
"closed " * f.path
endFurther resources: