# Computed Properties for Haskell Records

DevFeed: [Computed Properties for Haskell Records](<https://devfeed.tech/articles/computed-properties-for-haskell-records-27914.md>)

Original publisher: [Read original article](<http://alt-romes.github.io/posts/2023-11-30-computed-properties-for-haskell-records.html>)

Published: 2023-11-30T00:00:00Z

Content type: article

Language: en

Sources: [Romes' Musings](<https://devfeed.tech/sources/romes-musings.md>)

Topics: [Haskell](<https://devfeed.tech/topics/haskell.md>), [properties](<https://devfeed.tech/topics/properties.md>), [data type](<https://devfeed.tech/topics/data-type.md>), [Swift](<https://devfeed.tech/topics/swift.md>), [C](<https://devfeed.tech/topics/c.md>)

Tags: [c](<https://devfeed.tech/tags/c.md>), [data-type](<https://devfeed.tech/tags/data-type.md>), [extension](<https://devfeed.tech/tags/extension.md>), [haskell](<https://devfeed.tech/tags/haskell.md>), [properties](<https://devfeed.tech/tags/properties.md>), [rust](<https://devfeed.tech/tags/rust.md>), [swift](<https://devfeed.tech/tags/swift.md>)

## AI overview

This article explains Haskell record types and related syntax extensions, including OverloadedRecordDot and NamedFieldPuns. It then introduces computed properties by comparing Haskell records with properties in Swift, C#, and Java.

## Source excerpt

Contents 1 Records in Haskell 1.1 Overloaded Record Dot 1.2 Named Field Puns 2 Computed Properties 3 Conclusion 1 Records in Haskell Haskell has so-called record types, which are also commonly known as structs, for instance, in C, Swift, and Rust. To define a square, one would write: data Point = Point { x :: Int , y :: Int } data Square = Square { topLeft :: Point , bottomRight :: Point } mySquare = Square{ topLeft = Point{x = 0, y = 0} , bottomRight = Point{x = 2, y = 2} } mySquareWidth = x (bottomRight mySquare) - x (topLeft mySquare) In Haskell record types are just syntactic sugar for ordinary product types paired with functions that get and set these fields. In essence, the above is not fundamentally different from having the following standard product types and functions: data Point = Point Int Int data Square = Square Point Point x, y :: Point -> Int x (Point px _) = px y (Point _ py) = py topLeft, bottomRight :: Square -> Point topLeft (Square tl _) = tl bottomRight (Square _ br) = br -- And setters... 1.1 Overloaded Record Dot However, by turning on the OverloadedRecordDot syntax extension, you can use more syntactic sugar to project the fields of a record instead of using the field name as a standard function: {-# LANGUAGE OverloadedRecordDot #-} mySquareWidth = mySquare.bottomRight.x - mySquare.topLeft.x which is neat! I like OverloadedRecordDot. It looks clean and feels more like using proper property of the record data type. It is also less ambiguous for an LSP to suggest the record properties of a data type by typing after the ., than it is to suggest functions to apply to the record type argument. 1.2 Named Field Puns Since I'm already writing about records, I'll mention another extension I quite enjoy: NamedFieldPuns. Traditionally, when matching on a record, you can list the field names and bind variables to the value associated with that field. Continuing the above example: area :: Square -> Int area Square{topLeft = tl, bottomRight = br} = (br.x