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

Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
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
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
WolfDan
After doing a port from a c++ library to my project in phoenix I’ve seen that I need a faster way to run this algorithm and I found this ...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18146 126
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
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

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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 39297 209
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement