lorefnon

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

NobbZ

The more idiomatic way is to pass in the “compatible” module as an argument to Xs functions.

mbuhot

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

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

peerreynders

peerreynders

Tuple Calls - #16 by josevalim

For all purposes, it is an undocumented feature and it may be removed from Erlang anytime.

Tuple calls are already disabled by default on Erlang/OTP 21 and they explicitly require a @compile option on every module that needs them.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement