3

Is there a way to pattern match smart constructors, outside of its module?

Something like this:

import MyModule (thing)

fn (thing 3) = True

without being able to write this:

fn (Thing 3) = True

where thing is a smart constructor for Thing.

Filip Haglund
  • 13,919
  • 13
  • 64
  • 113
  • 1
    Possible duplicate of [Pattern matching on a private data constructor](http://stackoverflow.com/questions/33722381/pattern-matching-on-a-private-data-constructor) – Alexey Romanov Apr 23 '16 at 21:27

1 Answers1

8

Define this in MyModule and export it:

extract :: Thing -> Int
extract (Thing x) = x

Use the view patterns extension:

{-# LANGUAGE ViewPatterns #-}

fn :: Thing -> Bool
fn (extract -> 3) = True
gdejohn
  • 7,451
  • 1
  • 33
  • 49