Fl4m3Ph03n1x

Fl4m3Ph03n1x

Background

So I have a behaviour that has some functions with optional parameters and I want a way to make them more readable:

  @type order :: %{
          String.t() => String.t(),
          String.t() => String.t(),
          String.t() => non_neg_integer,
          String.t() => non_neg_integer,
          String.t() => non_neg_integer
        }
@type deps :: keyword

@callback place_order(order) ::
              {:ok, order_id}
              | {:error, :order_already_placed | :invalid_item_id, order}
@callback place_order(order, deps) ::
              {:ok, order_id}
              | {:error, :order_already_placed | :invalid_item_id, order}

In both cases my function will return exactly the same, the only difference is that I can either pass a keyword of dependencies, in which case the implementation of the behaviour will use, or I can leave it blank, in which case the implementation will use something by default.

The issue

The issue here is that I have a lot of repetition I dont need. I could just define more new types, one for @type ok_repsonse and one for @type error_response but not only is that not really needed, it also adds to the complexity and adds extra levels of indirection.

I was hoping there was something more like an optional keyword for callback parameters I could use:

@callback place_order(order, optional(deps)) ::
  {:ok, order_id} 
  | {:error, :order_already_placed | :invalid_item_id, order}

Question

How can I define optional parameters in a callback inside a behaviour without duplicating everything?

Showing Posts 1 to 8

LostKobrakai

LostKobrakai

Afaik what you’re looking for is not possible at the moment. Packages like ecto only put the full parameter version of their functions on behaviours, while the concrete implementations do implement optional params. In a recent experiment with mocking Ecto.Repo using Mox I found this to be unsatisfying, as one needs to not skip optional parameters in lots of places to not get in trouble with Mox. On the other hand the behaviour (/interface) clearly states that those functions need to have certain arguments, and one shouldn’t depend on the concrete implementation, but only the interface, when intending to switch out multiple implementations.

Marcus

Marcus

I am not sure, but the callback with all arguments should be enough.

If you implement the callback you will do something like that:

def place_order(order, opts \\ []) do ...

And for this you just needs:

@callback place_order(order, deps) ::
              {:ok, order_id}
              | {:error, :order_already_placed | :invalid_item_id, order}
Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

But then if I do that, Dialyzer and Hammox/Mox will complain saying I am not respecting the behaviour/interface I have defined and will error out. This is what I am trying to avoid.

LostKobrakai

LostKobrakai

One potential solution might be separating optional params from the concrete implementations:

# Behaviour
@callback my_fun(atom, keyword) :: atom

# Impl
def my_fun(atom, opts), do: Keyword.get(opts, :test, atom)

# Module in front of impl
def my_fun(impl, atom, opts \\ []), do: impl.my_fun(atom, opts)

This way the simpler API is easily sharable via all concrete implementations of a smaller behaviour.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

I am confused, could you give an example of how I could use this?
I don’t understand the

# Module in front of impl
def my_fun(impl, atom, opts \\ []), do: impl.my_fun(atom, opts)

function.

How would you invoke the implementation of the callback?

MyImplModule.my_fun(MyImplModule, order) 

and if I want to pass in dependencies:

MyImplModule.my_fun(MyImplModule, order, deps) 

???

LostKobrakai

LostKobrakai

defmodule Http do
  def get(client, url, opts \\ []) do
    client.get(url, opts)
  end
end

defmodule Http.Client do
  @callback get(URI.t | String.t, keyword) :: term
end

defmodule Http.Httpoison do
  @behaviour Http.Client
  def get(url, opts) do
    HTTPoison.get(url, opts)
  end
end

You’d never actually call functions on Http.Httpoison directly, but only though Http.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

Alright, so I understand you would use a Mox that obeys the HttpClient behaviour which would replace the Httppoison client.

Now however this becomes an issue of boundaries. Where is the boundary in your system?
If we take for example, the hexagonal architecture, your port would be the Http.Client and your adapter Http.Httpoison, which means your boundary is Http.client (because it is your port).

Basically, your are pushing your boundaries one level away, adding an intermediary module. I guess technically it works, but it does seem to add quite a bit of complexity.

LostKobrakai

LostKobrakai

Correct. I’m not so sure about the complexity though. Yes, it’s “slightly more” code and especially a module more. Yes, you need to deal with passing the implementation as e.g. a parameter. But you’ll get lesser complexity for any of your implementations, as they don’t need to deal with any optionality/function heads and can just provide an implementation for a clean and simple behaviour. The part dealing with your optional params becomes part of your functional core and is therefore easier to test.

Basically – for the amount of code you need to have – this approach moves the line between your core and the ugly outside world further out / makes the edge smaller.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
jtormey
Lately I’ve been thinking about how to organize components as a LiveView application grows. One of the pain points I’ve found (for myself...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews