Maxximiliann

Maxximiliann

CompileError: "invalid use of _. "_" represents a value to be ignored in a pattern and cannot be used in expressions"

This case statement is perfectly functional, but it repeats code:

case foo do
  {:pass, _} = reason ->
    Logger.info(reason)

  {:ok, _, _} ->
    status = Keyword.get(opts, :status)
    some_func(status)

    Logger.info("Executing function: #{some_func} with status: #{status}")

  {:ok, :bosh} ->
    status = Keyword.get(opts, :status)
    some_func(status)

    Logger.info("Executing function: #{some_func} with status: #{status}")
end

To eliminate repetition, the following version was attempted, however it results in a CompileError: invalid use of _. "_" represents a value to be ignored in a pattern and cannot be used in expressions

case foo do
  {:pass, _} = reason ->
    Logger.info(reason)

  message
  when message == {:ok, _, _} or message == {:ok, :bosh} ->

    status = Keyword.get(opts, :status)
    some_func(status)

    Logger.info("Executing function: #{some_func} with status: #{status}")
end

What’s the best way to resolve this issue in a case statement? What alternate control flow structure, if any, would be more appropriate for this task?

Marked As Solved

eksperimental

eksperimental

You are breaking a few rules in your code.

  1. _ matches anything on the left side of a assignment, not on the right side, where you are defining your values.
  2. _ works with the = (match) operator not with the `==’ (comparison) operator.
  3. you cannot use the = operator in guards

Anyway, There are at least two ways of doing it, one with match? as @al2o3cr mentioned, another one with guards.

foo = {:ok, :bosh}

cond do
  match?({:pass, _}, foo) ->
    1

  match?({:ok, _, _}, foo) or match?({:ok, :bosh}, foo) ->
    2
end

case foo do
  {:pass, _} ->
    1

  foo
  when is_tuple(foo) and tuple_size(foo) == 2 and elem(foo, 0) == :ok
  when is_tuple(foo) and tuple_size(foo) == 3 and elem(foo, 0) == :ok and elem(foo, 1) == :bosh ->
    2

  # previous clause can be optimized like this which can be optimized like this (not so much readable as the previous one though)
  foo
  when is_tuple(foo) and elem(foo, 0) == :ok and
         (tuple_size(foo) == 2 or (tuple_size(foo) == 3 and elem(foo, 1) == :bosh)) ->
    2
end

There is a practical use for the cumbersome second solution, and that is GUARDS! you can define the guard with defguardand use it even in function definitions (since you asked about avoiding repetition)

  defguard is_foo(term)
           when is_tuple(term) and elem(term, 0) == :ok and
                  (tuple_size(term) == 2 or (tuple_size(term) == 3 and elem(term, 1) == :bosh))

case foo do
  {:pass, _} ->
    1

  foo when is_foo(foo)  ->
    2
end

Also Liked

soup

soup

Optimise for readability until it’s a bottleneck IMO.

e: disregard, see benches below

soup

soup

Actually thinking about it, the cond code isn’t very flexible, you’ll probably need to use match? in each clause.

I benched it as-is anyway:

code
x = {:ok, 1}

case_fn = fn ->
  case x do
    {:ok, _} -> :ok
    _ -> :error
  end
end

cond_fn = fn ->
  cond do
    {:ok, _} = x -> :ok
    true -> :error
  end
end

Benchee.run(
  %{
    "case" => case_fn,
    "cond" => cond_fn
  },
  time: 10,
  memory_time: 2
)
spoilers!
Operating System: Linux
CPU Information: Intel(R) Core(TM) i5-4670K CPU @ 3.40GHz
Number of Available Cores: 4
Available memory: 23.37 GB
Elixir 1.13.1
Erlang 24.2

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 10 s
memory time: 2 s
parallel: 1
inputs: none specified
Estimated total run time: 28 s

Benchmarking case...
Benchmarking cond...

Name           ips        average  deviation         median         99th %
case        3.50 M        0.29 μs ±18914.94%       0.166 μs        0.58 μs
cond        0.76 M        1.32 μs  ±3154.19%        1.02 μs        1.90 μs

Comparison: 
case        3.50 M
cond        0.76 M - 4.61x slower +1.03 μs

Memory usage statistics:

Name    Memory usage
case         0.23 KB
cond         1.42 KB - 6.07x memory usage +1.19 KB

**All measurements for memory usage were the same**

cond performs “slightly” worse, at 4.6x slower and 6x more memory but that sounds like an Operations problem to me. Just buy a bigger instance :slight_smile:.

al2o3cr

al2o3cr

What version of Elixir are you running? Trying the code from your post in 1.12.0 produces a different compile error:

** (CompileError) main.exs:14: invalid use of _. "_" represents a value to be ignored in a pattern and cannot be used in expressions
    (stdlib 3.15.2) lists.erl:1358: :lists.mapfoldl/3
    (stdlib 3.15.2) lists.erl:1359: :lists.mapfoldl/3
    (stdlib 3.15.2) lists.erl:1358: :lists.mapfoldl/3
    (elixir 1.12.2) expanding macro: Kernel.or/2

I can get the illegal expression in guard, case is not allowed message if I use match?({:ok, _, _}, message) instead of == in the guard.

soup

soup

Maintaining the case:

run_fn = fn ->
  status = Keyword.get(opts, :status)
  some_func(status)

  Logger.info("Executing function: #{some_func} with status: #{status}")
end

case foo do
  {:pass, _} = reason ->
    Logger.info(reason)

  {:ok, _, _} ->
    run_fn.()

  {:ok, :bosh} ->
    run_fn.()
end

Using with (this one will kill you one day)

with {:pass, _} = reason <- foo do
  Logger.info(reason)
else
  _ ->
    # one day this will run for {:error, :fatal}...
    status = Keyword.get(opts, :status)
    some_func(status)

    Logger.info("Executing function: #{some_func} with status: #{status}")
end

Safer with, though Keathley may make sad noises Good and Bad Elixir

with :ok <- elem(foo, 0) do
  status = Keyword.get(opts, :status)
  some_func(status)

  Logger.info("Executing function: #{some_func} with status: #{status}")
else
  :pass ->
    Logger.info(foo)
end
Maxximiliann

Maxximiliann

Thanks for taking the time to patiently explain why my refactor was so unsuccessful. I’ve opted to go with cond, but would case be a faster and more efficient option?

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this 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

We're in Beta

About us Mission Statement