Drathier
Drathier

Reputation: 14519

Pattern match using smart constructor

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.

Upvotes: 3

Views: 444

Answers (1)

gdejohn
gdejohn

Reputation: 7579

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

Upvotes: 8

Related Questions