Fl4m3Ph03n1x

Fl4m3Ph03n1x

How do define behaviours that have callbacks with optional parameters?

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?

Most Liked

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.

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.

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.

Where Next?

Popular in Discussions Top

CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement