RoyalIcing / Orb

Write WebAssembly with Elixir
https://useorb.dev
BSD 3-Clause "New" or "Revised" License
174 stars 1 forks source link

Add `guard`? #29

Open RoyalIcing opened 2 months ago

RoyalIcing commented 2 months ago

I have a confession… I don’t like unless — I find it confusing as I have to negate an if statement in my head.

A variation of unless that I really like is Swift’s guard. It’s essentially a check with an early return. Unless the guard’s condition is true, it then forces you to return on the else.

guard len > 0 else {
  return false
}

Here’s what it could look like with Orb:

# Rather than:
unless len > 0 do
  return(0)
end

# Instead write:
guard do
  len > 0
else
  return(0)
end

I’d be open to using unless if there’s some way to make it more readable.

Note that WebAssembly doesn’t have the concept of unless, only Elixir does. So I don’t see adding a new concept that WebAssembly doesn’t have like guard as that much more of mortal sin than adding unless.

Alternatives

unless(len > 0, return: 0)
return(0 when not len > 0)