Discussion:
[elm-discuss] Unexpected compiler behaviour and message "pattern is redundant".
jadski
2017-07-09 20:55:27 UTC
Permalink
Hi, I am getting a compiler error that does not make sense to me - can
anyone help to explain why this fails to compile?

The following code:

<code>
type Message = Good | Bad | ManyOthers

type alias Node = { message : Message }

trymatch : Node -> Message -> Bool
trymatch node messageParam =
case node.message of
messageParam ->
True
_ ->
False

evalTrue = trymatch { message = Good } Good
evalFalse = trymatch { message = Good } Bad
</code>

Generates this compilation error:

<code>
The following pattern is redundant. Remove it.

10| _ ->
^
Any value with this shape will be handled by a previous pattern.
</code>

However node.message and messageParam can clearly be different, as in
evalFalse.

Hence the message "Any value with this shape will be handled by a previous
pattern." appears incorrect, and is confusing me.

A helpful slacker suggested using "if node.message == messageParam then",
which compiles, but doesn't clarify what's going wrong here.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
jadski
2017-07-09 21:04:15 UTC
Permalink
With manual formatting

*Code*

type Message = Good | Bad | ManyOthers

type alias Node = { message : Message }

trymatch : Node -> Message -> Bool
trymatch node messageParam =
case node.message of
messageParam ->
True
_ ->
False

evalTrue = trymatch { message = Good } Good
evalFalse = trymatch { message = Good } Bad


*Error Message*

The following pattern is redundant. Remove it.

10| _ ->
^
Any value with this shape will be handled by a previous pattern.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David Andrews
2017-07-09 21:08:34 UTC
Permalink
`case` statements in elm can not match variables. Both of these branches
look to the compiler as if they match everything. It interprets
`messageParam` as the name of a variable to which to bind the match, which
in this case will be `node.message`.
Post by jadski
With manual formatting
*Code*
type Message = Good | Bad | ManyOthers
type alias Node = { message : Message }
trymatch : Node -> Message -> Bool
trymatch node messageParam =
case node.message of
messageParam ->
True
_ ->
False
evalTrue = trymatch { message = Good } Good
evalFalse = trymatch { message = Good } Bad
*Error Message*
The following pattern is redundant. Remove it.
10| _ ->
^
Any value with this shape will be handled by a previous pattern.
--
You received this message because you are subscribed to the Google Groups
"Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
jadski
2017-07-10 14:47:23 UTC
Permalink
Thanks. I wasn't aware of that - makes sense.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Patrick Stubner
2017-07-10 12:11:20 UTC
Permalink
I haven't really used Elm that much, but you may want something like this:


module Main exposing (..)

import Html exposing (Html, text, div)

type Message = Good | Bad | ManyOthers

type alias Node = { message : Message }

trymatch : Node -> Message -> Bool
*trymatch node messageParam =*
* node.message == messageParam*

evalTrue = trymatch { message = Good } Good
evalFalse = trymatch { message = Good } Bad

main : Html a
main =
div []
[ text (toString evalTrue)
, text " / "
, text (toString evalFalse)]


You can try it out in https://ellie-app.com
Post by jadski
Hi, I am getting a compiler error that does not make sense to me - can
anyone help to explain why this fails to compile?
<code>
type Message = Good | Bad | ManyOthers
type alias Node = { message : Message }
trymatch : Node -> Message -> Bool
trymatch node messageParam =
case node.message of
messageParam ->
True
_ ->
False
evalTrue = trymatch { message = Good } Good
evalFalse = trymatch { message = Good } Bad
</code>
<code>
The following pattern is redundant. Remove it.
10| _ ->
^
Any value with this shape will be handled by a previous pattern.
</code>
However node.message and messageParam can clearly be different, as in
evalFalse.
Hence the message "Any value with this shape will be handled by a previous
pattern." appears incorrect, and is confusing me.
A helpful slacker suggested using "if node.message == messageParam then",
which compiles, but doesn't clarify what's going wrong here.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Андрей Коппель
2017-07-22 07:41:59 UTC
Permalink
Hello. The compiler gives here correct message. Because your messageParam can be anything of type Message. Lodash(_) in Elm means the same. So it means anything with type Message. That’s why you have two same branches. And Elm compiler tells you about it.

Best,
Andrey
Post by Patrick Stubner
module Main exposing (..)
import Html exposing (Html, text, div)
type Message = Good | Bad | ManyOthers
type alias Node = { message : Message }
trymatch : Node -> Message -> Bool
trymatch node messageParam =
node.message == messageParam
evalTrue = trymatch { message = Good } Good
evalFalse = trymatch { message = Good } Bad
main : Html a
main =
div []
[ text (toString evalTrue)
, text " / "
, text (toString evalFalse)]
You can try it out in https://ellie-app.com
Hi, I am getting a compiler error that does not make sense to me - can anyone help to explain why this fails to compile?
<code>
type Message = Good | Bad | ManyOthers
type alias Node = { message : Message }
trymatch : Node -> Message -> Bool
trymatch node messageParam =
case node.message of
messageParam ->
True
_ ->
False
evalTrue = trymatch { message = Good } Good
evalFalse = trymatch { message = Good } Bad
</code>
<code>
The following pattern is redundant. Remove it.
10| _ ->
^
Any value with this shape will be handled by a previous pattern.
</code>
However node.message and messageParam can clearly be different, as in evalFalse.
Hence the message "Any value with this shape will be handled by a previous pattern." appears incorrect, and is confusing me.
A helpful slacker suggested using "if node.message == messageParam then", which compiles, but doesn't clarify what's going wrong here.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...