Loading up local variables from record

If I have a record, say

type T = { value1 :: Int, value2 :: Number }, and I pattern match it in a function like this:

foo :: T -> Number
foo { value1: value1, value2: value2 } = toNumber value1 + value2

See how I assigned local variables using the same names as the record fields? Is there a shorthand for that?

Mike

There is sugar called “record field puns” that allows you to do that. For your example, you could write it as

foo :: T -> Number
foo { value1, value2 } = toNumber value1 + value2
3 Likes

you can also do:

let
  x = 1
  y = 2
in {x, y}