Remove 1 record field

(0) @DullBananas has asked two questions. The first, I think is this: Can I make a copy of a record, but with one field removed. This question @akoppela has answered, and I mostly agree with that answer. However, see (5) below. (Disclaimer: I’m new to Elm. My answer might contain an error.)

(1) The second question is

(2) The elm repl says that this can be done.

> type alias Goal a = { a | field : Int } -> a
> 
> solution : Goal { field : Int }
| solution = identity
|   
<function> : Goal { field : Int }

(3) But why do we get the following error from the compiler?

> puzzle : Goal { field : Int }
| puzzle rec = rec
|   
elm: Map.!: given key is not an element in the map
CallStack (from HasCallStack):
  error, called at libraries/containers/Data/Map/Internal.hs:610:17 in containers-0.5.11.0:Data.Map.Internal

I’ve raised a issue for this.

(4) Regarding the first question. There are two ways to create a record in Elm 0.19. The one is to use a literal, either directly or in a type alias. The second is to use a field update literal on an existing record.

Therefore inspection of the Elm files in a given app with give all the possible field names and, putting value types to one side, record types. So perhaps the record re-construction might be possible.

(5) Perhaps @DullBananas can achieve their goal by using

type alias Solution a = { a | field : Maybe Int } -> a

I’m interested to know why @DullBananas wants to remove a field from a record.

1 Like