Extend PureScript Records Safely with Row Polymorphism
Row polymorphism lets PureScript functions accept records with extra fields without sacrificing type safety or runtime performance.
17 Nov 2025, 04:08 UTC

The problem: a config type that refuses to grow
Imagine you are maintaining a PureScript service that reads configuration from a record. Your first version looks like this:
type Config = { name :: String, port :: Int }
readName :: Config -> String
readName config = config.name
This works until a teammate needs to pass debug :: Boolean alongside name and port. Because Config is a closed record type, the compiler rejects any record with additional fields. You can either add every conceivable field to Config up front, or change the function signature every time a caller wants one more field. Both choices make the API brittle.
The decision: use row polymorphism for open records
PureScript supports row polymorphism: a way to write types that describe a record by the fields it must have, while leaving room for other fields. The extra fields are captured in a row variable, conventionally written r. The compiler checks that required fields exist at compile time, then erases the row information so the generated JavaScript performs only the field accesses you actually wrote.
Here is the same function rewritten to accept any record that contains at least a name field:
readName :: forall r. { name :: String | r } -> String
readName config = config.name
The type { name :: String | r } reads as “a record with a name field of type String, plus whatever other fields are in row r.” Because r is universally quantified, callers can pass records with extra fields and the function still type-checks.
Worked example: a loader that accepts extra configuration
Create a new PureScript project or use an existing one. From the project root, run:
spago init
This command creates spago.dhall and packages.dhall in the current directory. It requires write permission to the project folder and no network access unless you subsequently install packages.
Add a source file at src/Main.purs:
module Main where
import Prelude
import Effect (Effect)
import Effect.Class.Console (log)
readName :: forall r. { name :: String | r } -> String
readName config = config.name
main :: Effect Unit
main = do
let simple = { name: "app" }
let extended = { name: "app", port: 8080, debug: true }
log $ readName simple
log $ readName extended
Compile it:
spago build
Expected check: the build succeeds. Both simple and extended satisfy the { name :: String | r } constraint because r absorbs { port :: Int, debug :: Boolean } in the second case.
To see the constraint enforce itself, change readName to access a field that is not in the required row:
readName :: forall r. { name :: String | r } -> String
readName config = config.port
Run spago build again. The compiler should reject the program because port is not guaranteed by { name :: String | r }; it might be hidden inside r. This is the diagnostic that tells you the row variable is doing its job.
Trade-offs and limits
Row polymorphism is not free. Complex row constraints can produce cryptic type errors, especially when multiple functions combine records with different row variables. Pattern matching on a polymorphic record usually requires an explicit type annotation so the compiler can resolve which fields are concrete and which belong to the row.
For JavaScript interop, remember that row information exists only at compile time. PureScript records generally compile to plain JavaScript objects, but a library expecting an exact object shape may behave unexpectedly if your record carries extra fields or if you rely on field order.
Closing recommendation
Use row polymorphism when a function or API needs to read a few known fields from records that callers may extend. Keep the required fields explicit in the type signature, avoid pattern matching on polymorphic records unless you annotate them, and verify behavior with spago build after each change. If your team frequently hits confusing row errors, consider documenting the convention or falling back to concrete record types for internal data structures where extensibility is not required.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.