Purescript: Pattern match wildcard data constructor

Viewed 468

While the example is contrived, why can I not use the wildcard pattern if the data constructor is ignored?

module Main where

import Prelude
import Control.Monad.Eff.Console (log)

data Person = Amy { name :: String  } | George { name :: String  }

--Implementations Options Below

main = log $ personToString $ George  { name: "George" }

No Error

personToString :: Person -> String
personToString (Amy { name: n }) = n
personToString (George { name: n }) = n

Error

personToString :: Person -> String
personToString (_ { name: n }) = n

http://try.purescript.org/?session=a1503b9a-0546-7832-39b0-6321a89ef2e3

Unable to parse module:
  unexpected {
  expecting ::, operator or )
2 Answers

If the constructor can safely be ignored, that's a smell that the type can be refactored:

data AmyOrGeorge = Amy | George

data Person = Person AmyOrGeorge { name :: String  }

personToString (Person _ { name: n }) = n

I agree with the language designers' choice to leave this feature out, because working around it actually improves the code.

Related