Hasty Briefsbeta

Bilingual

Prefer do notation over Applicative operators when assembling records (2024)

a day ago
  • #Haskell
  • #Code Ergonomics
  • #Functional Programming
  • Use do notation over Applicative operators (<$>/<*>) for assembling records in Haskell, applicable to both Monad and Applicative type constructors, with ApplicativeDo extension needed for the latter.
  • Benefits include ergonomic advantages, such as easier logic integration and clearer code, especially when adding prompts or explanations, making it more beginner-friendly.
  • Do notation is order-insensitive: reordering fields in a datatype doesn't break the code, unlike with Applicative operators where silent breakage occurs.
  • Better error messages are provided when adding new fields to a record; missing fields are clearly indicated without requiring understanding of curried function types.
  • The approach requires record syntax datatypes and doesn't work for positional arguments or arbitrary functions; ApplicativeDo enables usage for non-Monad Applicatives like optparse-applicative parsers.