Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern match using smart constructor

Tags:

haskell

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.

like image 483
Filip Haglund Avatar asked Feb 01 '26 02:02

Filip Haglund


1 Answers

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
like image 195
gdejohn Avatar answered Feb 02 '26 14:02

gdejohn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!