#Records with inconsistent fields

1 messages · Page 1 of 1 (latest)

cursive mural
#

The bellow code reports the last_visited_at a field as missing, even if it is defined in all record variants:

pub type User {
  User(username: String, last_visited_at: String)
  Visitor(last_visited_at: String)
}

pub fn main() {
  let users = [
    User(username: "Robert", last_visited_at: "2019-11-16T16:00:33.52Z"),
    Visitor(last_visited_at: "2019-11-16T10:11:02.66Z")
  ]

  list.each(users, fn(user: User) {
    io.println("user.last_visited_at:" <> user.last_visited_at)
  })
}

Question 1 : Am I wrong trying to use record variants to make my code behaving differently based on the record variant?
Question 2: Is this something that would be supported later by gleam?
Question 3: What would be a better way of achieving this in gleam?

Thank you in advance!

fallow roost
#

The fields aren't in the same position so it can't allow it

cursive mural
#

Oh! You're right, placing them in the same order "fixed" the error. Thank you!

#

So it's fine to use record variants like that?

fallow roost
#

I've personally never used it, but yeah I would say so

#

It's specially implemented in the compiler, so it would be weird to discourage it

sterile wren
#

Yeah I totally take advantage of that feature in my code

sharp pelican
#

seems odd to me that field order would matter in this context