Remix.run Logo
ngruhn 12 hours ago

I can live with these negatives. What irritates me the most is the lack of if/else or guards or some kind of dedicated case-distinction on booleans. Pattern matching is great but for booleans it can be kinda verbose. E.g.

   case x < 0 {
     True -> ...
     False -> 
       case x > 10 {
          True -> ...
          False -> 
            case x <= 10 {
               True -> ...
               False -> ...
            }
       }
   }
WJW 11 hours ago | parent | next [-]

There are (some) guards available though? You could rewrite your example as:

    case x {
      n if x < 0 -> ...
      n if x > 10 -> ...
      n if x <= 10 -> ...
    }
Guards are a bit limited in that they cannot contain function calls, but that's a problem of the BEAM and not something Gleam could control.
ngruhn 11 hours ago | parent | next [-]

Ah right, I remember now.

> Guards are a bit limited in that they cannot contain function calls,

I feel like that's not a small sacrifice.

> but that's a problem of the BEAM and not something Gleam could control.

Could Gleam desugar to a case expression like I wrote above?

tymscar 11 hours ago | parent | prev [-]

You most likely asked an AI for this. They always think there is an `if` keyword in case statements in Gleam. There isn't one, sadly.

EDIT: I am wrong. Apparently there are, but it's a bit of a strange thing where they can only be used as clauses in `if` statements, and without doing any calculations.

WJW 11 hours ago | parent [-]

There is though?

https://tour.gleam.run/flow-control/guards/

lpil 10 hours ago | parent | prev [-]

In Gleam we would typically write this code with `use`, which doesn’t introduce nesting.