Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to construct a new record using Lenses?

Tags:

haskell

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?

like image 903
Peter Hall Avatar asked Aug 25 '13 15:08

Peter Hall


1 Answers

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.

like image 166
bennofs Avatar answered Oct 31 '22 19:10

bennofs