Is there any method of constructing a value for recording with lenses without underscore identifiers?

For example, I have the following entry

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

makeLenses ''Rec

and I see only 2 ways to build new values:

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

The second option doesn’t look very good if the number of record fields is more than a couple, and the underscores do not look natural in the first.

Are there other ways to create record values?

+4
source share
1 answer

If that makes sense for your type, an Defaultinstance might be a good approach. Then you can do

   def & a.~1
       . b.~2
       . c.~3
+4
source

Source: https://habr.com/ru/post/1523899/


All Articles