Row Polymorphism in PureScript: Accept Extra Fields Without Boilerplate
Use PureScript row polymorphism to write functions that accept records with extra fields, eliminating conversion boilerplate between layers.
17 Nov 2025, 13:30 UTC

The duplication tax on API records
You are building a PureScript API. Your core domain defines a user record:
type User = { id :: UserId, name :: String, email :: Email }
Your authentication layer adds a token:
type AuthenticatedUser = { id :: UserId, name :: String, email :: Email, token :: Token }
Now you want a logging function that only needs the user's ID. Without row polymorphism, you either write the function against AuthenticatedUser and cannot reuse it for plain User, or you write conversion functions to copy fields between record types. That boilerplate grows with every new layer.
The thesis: describe fields you need, not the whole record
PureScript's row polymorphism lets a function declare that it accepts a record with at least certain fields. The extra fields are captured by a row variable, conventionally written r. A row is the unnamed tail of a record type—the fields that remain after you name the ones you care about.
Here is the logging function:
logUserId :: forall r. { userId :: String | r } -> Effect Unit
Read this as: for any row r, accept a record that has a userId field of type String, plus whatever other fields r contains. You can pass a plain User, an AuthenticatedUser, or a record with many extra fields, and the compiler accepts all of them.
Worked example: middleware that reads any request context
Imagine an HTTP request context that accumulates fields as middleware runs. Early middleware adds a request ID; later middleware adds a user ID. You want a function to read the request ID without knowing every field that might be present.
module Main where
import Effect (Effect)
import Effect.Console (log)
type RequestContext r = { requestId :: String | r }
logRequestId :: forall r. RequestContext r -> Effect Unit
logRequestId ctx = log ctx.requestId
Create a Spago project in your terminal and run it from an empty project directory as a normal user; no special permissions are required:
spago init
spago install purescript-console
spago build
spago init creates spago.dhall and a src directory. After installing purescript-console, spago build should compile without errors. The operation is not destructive. If you pass a record missing requestId, the compiler rejects it before you run anything.
Call sites can now pass records with unrelated fields:
logRequestId { requestId = "abc-123", userId = "user-42", path = "/home" }
The row variable r absorbs userId and path. No conversion function is needed between a minimal context and an enriched one.
Trade-offs and limits
Row polymorphism is compile-time only. You cannot enumerate a record's fields at runtime without separate metadata, such as Symbol proxies or a generic derivation library. If your code needs to inspect field names dynamically, row polymorphism alone will not help.
Compiler errors can also be long. When a row constraint fails, PureScript may print the full inferred row rather than a concise "missing field" message. Add explicit type annotations to top-level functions to keep errors readable.
Finally, PureScript functions compile to monomorphic JavaScript. A row-polymorphic function does not become a generic JavaScript function that inspects objects at runtime. If you call PureScript from JavaScript through the FFI, write explicit conversion wrappers for each concrete record shape.
Check your work
Add a type annotation to your function, then deliberately omit the required field in a test call:
logRequestId { userId = "user-42" }
Run spago build again. A useful result is a compile error mentioning the missing requestId field. If the build succeeds, your function signature is too permissive and needs the required field in the row.
Closing
Row polymorphism lets you model APIs as extensible records instead of fixed shapes. Start with the fields a function actually needs, capture the rest in a row variable, and let the compiler enforce presence without conversion boilerplate.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.