lorefnon
What is the idiomatic solution for parameterizing modules?
OCaml provides a facility to parameterize modules (modules which accept other modules as arguments) through functors (this is different from functor algebraic data type).
Example:
module Increment (M : X_int) : X_int = struct
let x = M.x + 1
end;;
I am curious if there is a popular idiom similar to this in elixir, where a module can depend on multiple other modules, which can be swapped in later (potentially with different module arguments) to get composite modules.
Most Liked
NobbZ
The more idiomatic way is to pass in the “compatible” module as an argument to Xs functions.
mbuhot
There is an idiom used when a library defines a module that must be instantiated by the application that uses it, along with some configuration.
Ecto.Repo is an example:
defmodule MyApp.Repo
use Ecto.Repo, otp_app: MyApp
end
The otp_app keyword argument allows Ecto.Repo to lookup the remaining configuration with Application.get_env(app, __MODULE__)
In simpler cases the config can be passed directly into use.
OvermindDL1
You can pass module atom names around all you want and call them directly, so yes. (The OCaml Witness Pattern via First-Class modules)
If however you are refining a module based on another module (a limited form of OCaml Functors), Elixir/Erlang does have first-class support for that currently in the form of tuple-calls, however that feature is being broken soon much to my irritance… >.>







