Search code examples
haskelllenses

Is it possible to construct a new record using Lenses?


If I have a record type with lenses, is it possible to construct a new record without using the underlying record accessors?

{-# LANGUAGE TemplateHaskell #-}

import Control.Lens
import Control.Lens.TH

data Foo = Foo { _s :: String
               , _b :: Bool
               } deriving (Show, Eq)

makeLenses ''Foo

I could make Foo an instance of Data.Default and then modifiy def with lenses, but not all record types will have sensible defaults. Does Control.Lens have its own way to do it?


Solution

  • No, there is currently no way to do that. You'll have to use something like Foo{} as default or not use lens for record construction. However, there is already an issue in lens covering this.