#Nested `case` vs. Multiple subjects preference

1 messages · Page 1 of 1 (latest)

drowsy zodiac
#

A question on idiomatic Gleam: is there a general preference between nesting case expressions vs. using multiple subjects?

An illustrative example:

Nested:

case status {
  Ok(user) -> {
    case is_admin {
      True -> show_admin_panel(user)
      False -> show_home(user)
    }
  }
  Error(_) -> redirect_to_login()
}

With multiple subjects:

case status, is_admin {
  Ok(user), True -> show_admin_panel(user)
  Ok(user), False -> show_home(user)
  Error(_), _ -> redirect_to_login()
}

I was just manually refactoring some code from nested to multiple subjects, and it got me wondering if a "Convert to multiple subjects" Code Action in the LS would be an interesting proposal.

hasty haven
#

if anything you'd want both code actions I think

cause sometimes you might want multiple and other times you might not

so converting to and from like you can with pipes and 'use <-' would be the way imo

proper shell
#

not a direct answer about nested cases vs multiple subjects, but this similar code I ended up creating some helper fns to use with use
so the code is something like:
use required_user(smt)
use required_admin, etc
or it could be more generic, but again I'm still new to Gleam

maiden raft
#

I might write that like this

case status {
  Ok(user) if is_admin -> show_admin_panel(user)
  Ok(user) -> show_home(user)
  Error(_) -> redirect_to_login()
}