Search code examples
haskellhaskell-lens

Is there some method to construct value for record with lenses without underscore identifiers?


For example I have the following record

data Rec = Rec
    { _a :: Int
    , _b :: Int
    , _c :: Int
    } deriving (Show, Eq)

makeLenses ''Rec

and I see only 2 ways to constuct new values:

  • Rec{_a=1,_b=2,_c=3}
  • Rec 1 2 3

The second variant does not look good if the number of record fields is more than a pair and underscores does not look natural in the first one.

Are there any other ways to construct record values?


Solution

  • If that makes sense for your type, a Default instance might be a good approach. Then you can do

       def & a.~1
           . b.~2
           . c.~3