zlacker

[parent] [thread] 6 comments
1. ngruhn+(OP)[view] [source] 2025-12-13 19:31:33
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 -> ...
            }
       }
   }
replies(2): >>WJW+Y >>lpil+Vg
2. WJW+Y[view] [source] 2025-12-13 19:37:48
>>ngruhn+(OP)
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.
replies(2): >>tymsca+n1 >>ngruhn+I1
◧◩
3. tymsca+n1[view] [source] [discussion] 2025-12-13 19:39:53
>>WJW+Y
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.

replies(1): >>WJW+M2
◧◩
4. ngruhn+I1[view] [source] [discussion] 2025-12-13 19:42:03
>>WJW+Y
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?

replies(1): >>zelphi+Ul1
◧◩◪
5. WJW+M2[view] [source] [discussion] 2025-12-13 19:48:26
>>tymsca+n1
There is though?

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

6. lpil+Vg[view] [source] 2025-12-13 21:26:53
>>ngruhn+(OP)
In Gleam we would typically write this code with `use`, which doesn’t introduce nesting.
◧◩◪
7. zelphi+Ul1[view] [source] [discussion] 2025-12-14 11:44:12
>>ngruhn+I1
Guards also eliminate checking for whether a pattern matching is comprehensive. Or do I remember wrong and the language does not check it at all?
[go to top]