#pattern-matching on records, the spread operator, and "unsafe record update" error

1 messages · Page 1 of 1 (latest)

earnest pollen
#
pub type Widget
{
  Bracket(name: String, model: String)
  Sprocket(diameter: Float, thickness: Float)
}

fn update_widget(widget: Widget) -> Widget {
  case widget {
    Bracket(..) as b -> Bracket(..b, name: "new name")
    Sprocket(..) as s -> Sprocket(..s, diameter: 2*s.diameter)
  }
}

produces the error


I can't tell this is always the right constructor.

This type has multiple constructors so it cannot be safely updated.
If this value was one of the other variants then the update would be
produce incorrect results.

at Bracket(..b, name: "new name") -- does Gleam not have enough type info to know that b is a Bracket specifically?

median zenith
#

It does not, no

#

This requires a type system feature called type narrowing, which we do want, but no one has done any work on

#

There's an issue for it on github