[Haskell-cafe] incomplete-uni-patterns

Viktor Dukhovni ietf-dane at dukhovni.org
Thu Jan 26 04:03:23 UTC 2023


On Thu, Jan 26, 2023 at 09:05:40AM +0900, Kazu Yamamoto (山本和彦) via Haskell-Cafe wrote:

> Hello,
> 
> The recent GHC added incomplete-uni-patterns to the -Wall option.
> So, we have a waning with the following code:
> 
>     let [addr,port] = args
> 
> To avoid this, I changed the code to:
> 
>     let addr = head args
>         port = head $ tail args

The univeral option type is Maybe, so you leverage that:

    maybePair :: [a] -> Maybe (a, a)
    maybePair [a, b]    = Just (a, b)
    maybePair _         = Nothing

    knownPair :: [a] -> (a, a)
    knownPair           = fromJust . maybePair

and then anywhere you need to only match a 2-element list:

    let (a, b) = knownPair args

Or just:

    {-# OPTIONS_GHC -Wall -Wno-incomplete-uni-patterns #-}

    let [a, b] knownPair args

Perhaps mildly inconvenient at times, but I am inlined to think that
overall better than never reporting potential problem patterns.

-- 
    Viktor.


More information about the Haskell-Cafe mailing list