Elm merge two different records into one large one

Viewed 49

In Elm , is it possible to do something like this.

REPL

> a = {d = 4 , y = 5}
{ d = 4, y = 5 }
    : { d : number, y : number1 }
> b = {b = 4,n=1}
{ b = 4, n = 1 }
    : { b : number, n : number1 }
> c = {a | b | g=5}
 

I want c to contain all members of a and of b without individually naming them.

Is this possible?

Thanks

1 Answers

No, Elm does not support changing the shape of a record using the record update syntax (though it was possible before Elm 0.19), and there is nothing similar to the … spread syntax in JS.

You will need to use explicit syntax to copy each field into a new record.

type alias One =
    { d : Int, y : Int }


type alias Two =
    { b : Int, n : Int }


type alias Three =
    { d : Int, y : Int, b : Int, n : Int, g : Int }


merge : One -> Two -> Three
merge { d, y } { b, n } =
    { d = d, y = y, b = b, n = n, g = 5 }
Related