#How to do record update with `case record { Record(..) -> Record(..record, field: 1) }` syntax

1 messages · Page 1 of 1 (latest)

jovial sandal
#

I have this code:

pub fn from(query: Query(a), table: String) -> Query(a) {
  case query {
    // Select(what, decoder, from, where) -> Select(what, decoder, From(table), where)
    Select(..) -> Select(..query, from: From(table))
    CreateTable(..) -> panic
  }
}

it produces the following error

error: Unsafe record update
   ┌─ /home/grfork/reps/wind_world_rpg/src/sql/yoursql.gleam:77:19
   │
77 │     Select(..) -> Select(..query, from: From(table))
   │                   ^^^^^^ 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.

Consider pattern matching on it with a case expression and then
constructing a new record with its values.

How do I use the syntax correctly here? The syntax with destructuring every single field works fine, it is commented in the code

river laurel
#

you gotta do each field sperately unfortunately, afaik if the type has more than one constructor you can never use the update syntax

jovial sandal
#

I see I see, thanks

river laurel
jovial sandal
#

Oh my, it needs a full exhaustiveness issue to be completed

foggy prawn
#

Which is nearly done 🙂

floral echo
#

would as work here?

foggy prawn
#

There's no type narrowing so it's never possible atm

floral echo
#

oh, I guess that's why haskell uses x { } notation

foggy prawn
#

It's more that we unify the sum and product types

#

Though they do have type narrowing for sum types

floral echo
#

are you sure? records unify that too

#
ghci> data Foo = Foo { a::Int } | Bar { b::String } deriving Show
ghci> :t \x -> x { b = "foo" }
\x -> x { b = "foo" } :: Foo -> Foo
#

it just crashes if you apply the wrong tag

#

it also works if the fields have the same name, it's purely syntactic

foggy prawn
#

It does, but it doesn't emit the errors by default

#

You need to turn them on

#

SPJ did a really good talk about the algorithm they used now for this + exhaustiveness checking

toxic nebula
#

Are you building a query builder? I was just planning to make one myself 🤩

jovial sandal
toxic nebula
#

Awesome, would love to see how it works at some point :)