Skip to content

jaredly/ppx_guard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ppx_guard

guard let for reason/ocaml!

What?

A shortcut for early returns when you know it's going south :P

let doit something => {
  [%guard let Ok value = doAnAction something][@else None];
  /* etc */
  Some 10
}
/* becomes */
let doit something => {
  switch (doAnAction something) {
    | Ok value => Some 10
    | _ => None
  }
}


let multiple x y => {
  [%guard let Some x = x][@else None];
  let awesome = 10;
  [%guard let Some y = y][@else None];
  Some (x + y)
}
/* becomes */
let multiple x y => {
  switch x {
    | Some x => {
      let awesome = 10;
      switch y {
        | Some y => Some (x + y)
        | _ => None
      }
    }
    | _ => None
  }
}

Related work / inspiration

Inspired by swift's guard let, if let, if case, guard case, etc. It currently acts similar to swift's guard case.

It's also inspired by rust's try! macro (which is now deprecated in favor of a bit of syntax sugar, the ? postfix operator, which does the same thing).

It might be interesting to add a [%try function, which would have the same effect as rust's try macro. If we start to use result a lot, it could be worth it.

FAQ

Why not have it act only on option (as swift does with guard let)?

I considered that, but I think it might not be as common as in swift. An alternative would be to have a guard_case that does what this currently does, and then guard would be for unwrapping optionals.

About

early returns for ocaml/reason

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published