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

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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement