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

StackOverflow https://stackoverflow.com/questions/21381306

  •  03-10-2022
  •  | 
  •  

Вопрос

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?

Это было полезно?

Решение

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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top