Skip to content

Instantly share code, notes, and snippets.

@davidallsopp
Created June 17, 2015 21:54
Show Gist options
  • Save davidallsopp/fc67b34dc8dc06f062b2 to your computer and use it in GitHub Desktop.
Save davidallsopp/fc67b34dc8dc06f062b2 to your computer and use it in GitHub Desktop.
Constructor fields in Haskell - see https://www.haskell.org/tutorial/moretypes.html, section 6.2 Field Labels
Prelude> data Point = Point {x :: Int} deriving Show
Prelude> Point 1
Point {x = 1}
Prelude> :t x
x :: Point -> Int
Prelude> :t Point
Point :: Int -> Point
Prelude> let p = Point 2
Prelude> x p
2
@davidallsopp
Copy link
Author

The field names can be used as selector functions to extract a component from a structure. In this simple case, with just one parameter, the accessor x is the inverse of the data Constructor Point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment