Is it possible to define type that enchances record with maybe

Hello all,

I’m curious if it is possible in purescript to do something like derive type from record that add to all fields maybe, for instance

type Person = { name :: String, age :: Number }

-- Something Person that will result in type { name :: Maybe String, age :: Maybe Number }

Is there way to do that?

2 Likes

You can template it out with higher-kinded type synonyms.

type PersonFields f =
  ( name :: f String
  , age :: f Number
  )

-- You can pass synonyms like this to other type synonyms
type Unlifted a = a

type Person = { | PersonFields Unlifted }
type PersonMaybe = { | PersonFields Maybe }
5 Likes

There are more types you can use for f besides Maybe: https://jordanmartinez.github.io/purescript-jordans-reference-site/content/31-Design-Patterns/23-Higher-Kinded-Data.html

2 Likes

Thank you, thank link is very usefull

1 Like