Inherit provides a way to create pseudo-inheritance in Elixir by allowing modules to inherit struct fields and delegate function calls from other modules.
- Struct inheritance: Child modules inherit all fields from parent modules
- Function delegation: Public functions from parent modules are automatically delegated
- Field merging: Child modules can add additional fields to inherited structs
- Overridable functions: Inherited functions can be overridden in child modules
Use Inherit
in your module and define struct fields:
defmodule Person do
use Inherit, [
name: "",
age: 0
]
def greet(person) do
"Hello, I'm #{person.name} and I'm #{person.age} years old"
end
def adult?(person) do
person.age >= 18
end
end
Use the parent module in your child module and specify additional fields:
defmodule Employee do
use Person, [
salary: 0,
department: ""
]
# Override parent function
def greet(employee) do
"Hi, I'm #{employee.name}, I work in #{employee.department}"
end
end
# Create an Employee struct with inherited fields
employee = %Employee{
name: "John",
age: 30,
salary: 50000,
department: "Engineering"
}
# Call overridden function
Employee.greet(employee)
# => "Hi, I'm John, I work in Engineering"
# Call inherited function
Employee.adult?(employee)
# => true
- Parent module setup: When you
use Inherit
, the module becomes inheritable and defines its own struct - Inheritance: When you
use ParentModule
, the child module:- Inherits all struct fields from the parent
- Adds any additional fields specified
- Delegates all public functions from the parent module
- Makes inherited functions overridable
If available in Hex, the package can be installed
by adding inherit
to your list of dependencies in mix.exs
:
def deps do
[
{:inherit, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/inherit.