jedeleh

jedeleh

Compiler warning "this clause cannot match because a previous clause"

Good morning

I’m seeing this warning at compile time and while I know what the issue is, the warning message doesn’t give me much to go on.

My situation (vastly simplified) is that I’ve got a module that implements various versions of a function. Let’s call that function parse_line/3. I also have some other versions of that function in a module (to be shared across different modules). Here’s the main module:

defmodule UseThisModule do 
  defmacro __using__(_opts) do
    quote do
      def parse_line("a", b, c), do: ...
      def parse_line(a, "b", c), do: ...
     end
  end
end

And here is the module that uses it:

defmodule NeedsThatModule do
  use UseThisModule

  def parse_lin("a", 234, _), do: ...

end

It’s pretty easy to see that the parse_line in NeedsThatModule is going to never be found because of the first one in UseThisModule. I understand that and I know how to deal with it. My problem is that in UseThisModule there are 10+ versions of parse_line/3 and in NeatModule there are 15+. The warning says, as expected:

“warning: this clause cannot match because a previous clause at line 4 always matches
neat_module.ex:2”

In my example, line 2 is where the “use” expression is found. Conceptually, I know what is going on, but given the sheer number of versions of the parse_line/3 function, it’s extremely painful to identify WHICH of the versions in UseThisModule is the one to check on.

I guess I’m looking for strategies to simplify this situation, or even better, a way to get more details in the warning.

At any rate, if this is unclear, let me know and I’ll try again :slight_smile:
thanks

Most Liked

al2o3cr

al2o3cr

Sounds like you want to pass location: :keep to quote, that may give you a better stack trace according to the docs.

To me this sounds like you have too many heads for parse_line, given that you’re struggling to keep track of them. Doubly-so because you’re apparently building more than one giant parse_line function.

jedeleh

jedeleh

oh, perfect! I’ll look into this.

And yeah, there are a TON of heads for parse_line. We’re basically using them to parse lines without having to write more formal parsers. For example, we’ve got things like this:

# KEY/2323 VALUE/something

def parse_line(<<"KEY/", the_key::binary_size(4), " VALUE/", value::binary>>, _, _), do: %Output{key: key, value: value)

and so on and so forth.

And thank you for the suggestion! Much appreciated.

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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

Other popular topics 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
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
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