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

Last Post!

OvermindDL1

OvermindDL1

No it’s still there, just you have to set a compile option on the file to enable it’s usage. I’m quite angry at that though, it’s more useless work for the user…

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New

Other popular topics 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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement