saleyn

saleyn

Need help with a 'required' macro

Is there a way to create a macro called required that could be used like this:

with required(var) <- Map.get(map, key) do
  var
else
  nil -> raise RuntimeError
end

I tried something like this, but it doesn’t work:

defmacro required(var) do
  quote do
    unquote(var) when not is_nil(var)
  end
end

Most Liked

kip

kip

ex_cldr Core Team

I don’t think a macro can return just the match part of a with (but happy to be wrong). Nevertheless, this looks more complex that the use case demands. Map.fetch!/2 does basically the same thing:

iex> Map.fetch!(%{a: "a"}, :a)
"a"
iex> Map.fetch!(%{a: "a"}, :b)
** (KeyError) key :b not found in: %{a: "a"}
    (stdlib 4.0) :maps.get(:b, %{a: "a"})

so you could simply:

with value <- Map.fetch!(map, key) do
  do_something()
end

Even in this case, its not very idiomatic since with’s value is to program “happy path” and therefore the with seems redundant in this example and the following would be the simplest:

var = Map.fetch!(%{a: "a"}, :a)
kip

kip

ex_cldr Core Team

Perhaps I’m missing something, but this is very similar (but not the same) to:

  with \
    {:ok, p1} <- Map.fetch(map, :param1), 
    {:ok, p2} <- Map.fetch(map, :param2),
    ...,
    {:ok, pn} <- Map.fetch(map, :paramN)
  do
    ...
  end

The difference being that if the key exists and its value is nil then it will still pass unlike your example. Normally when I see use cases like “required” its a data validation case and changesets become a good tool of choice.

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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
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