[Haskell-cafe] Why are field selectors functions?

David Banas capn.freako at gmail.com
Fri Aug 9 15:49:58 CEST 2013


Wouldn't the implementation hiding feature of the *newtype *idiom be
broken, if field selectors were not first class functions? For instance,
the following code (taken shamelessly from Ch. 10 of *Real World Haskell*):

module Parse (
    runParser
) where

data ParseState = ParseState {
    string :: String
} deriving (Show)

newtype Parser a = Parser {
    runParser :: ParseState -> Either String (a, ParseState)
}


has the attractive feature of hiding the internal implementation of
the *ParseState
*and *Parser *types from the user, preventing him from, for instance,
pattern matching on either and thus writing code, which may break when we
change the implementation. I believe this is only possible, because
the *runParser
*accessor is exportable as a first class function.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130809/4fc05c54/attachment.htm>


More information about the Haskell-Cafe mailing list