Skip to content

mind6-fifthwave/Inherit.jl

Repository files navigation

Build Status Coverage

Inherit.jl

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.

Highlights

  • @abstractbase declares an abstract supertype with inheritable fields and required method signatures.
  • @implement defines a concrete subtype that inherits its supertype's fields and is required to satisfy every inherited method declaration.
  • @verify_interfaces is 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.close to 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.

Quick start

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.0

A 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
end

Further resources:

About

Inherit fields and method interfaces in Julia

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages