Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I disable the "non-exhaustive pattern matches" warning only for lambdas?

Can I disable the non-exhaustive pattern matches warning only for lambdas?

I like the warning in general, but not for actual lambda literals like this:

map (\(x:xs)->...) ls

I think this code makes it pretty clear that I expect all the values of ls to always have at least one element, and there is no neat way to handle the error case in the lambda. (I guess I could move the pattern match into a case statement, but that would just be ugly.)

like image 966
Tikhon Jelvis Avatar asked Feb 05 '12 09:02

Tikhon Jelvis


2 Answers

You could write

{-# LANGUAGE LambdaCase #-}
map (\case (x:xs)->...; [] -> error "wut") ls

In "wut" you could then describe why that should not have happened.

like image 117
Jeffrey Benjamin Brown Avatar answered Sep 28 '22 18:09

Jeffrey Benjamin Brown


Do you have such situations quite often? This is a code smell IMHO. I'd like to see some such lambdas and I am quite sure we can make a better version that also handles empty lists quite fine. And in all other cases you might go for a NonEmpty list type wrapper.

like image 28
Ingo Avatar answered Sep 28 '22 17:09

Ingo