Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pattern match against Text without using OverloadedStrings?

This works:

{-# LANGUAGE OverloadedStrings #-}
myFunc :: Text -> String
myFunc ""    = "nothing"
myFunc other = "something!"

Without the OverloadedStrings extension however, "" is of type String so it doesn't compile. Using a function like myFunc (pack "") is not allowed in patterns.

Haskell Bytestrings: How to pattern match? contains some suggestions that should work, however in this case I'm wondering if there is something special about the fact that it works with OverloadedStrings that would allow a better way?

like image 436
Xavier Shay Avatar asked Jun 07 '26 13:06

Xavier Shay


1 Answers

The most direct translation is with ViewPatterns

{-# LANGUAGE ViewPatterns #-}
import qualified Data.Text as Txt
myFunc (Txt.unpack->"") = "nothing"
myFunc _other = "something!"

The best translation, albeit probably too specific for your actual use case, is of course

myFunc txt | Txt.null txt  = "nothing"
           | otherwise     = "something!"

You could also go nuts and make up a pattern synonym:

{-# LANGUAGE PatternSynonyms, ViewPatterns #-}
pattern T :: String -> Txt.Text
pattern T str <- (Txt.unpack -> str)
 where T = Txt.pack

and then

myFunc (T"") = "nothing"
myFunc _other = "something"

Arguably, OverloadedStrings is a more sane extension than ViewPatterns, and certainly saner than PatternSynonyms.

like image 177
leftaroundabout Avatar answered Jun 10 '26 19:06

leftaroundabout