A function that can fail, its failures named, and otherwise handling them.
failures.zg
# A function that can fail, its failures named, and `otherwise` handling them.structLocker{owner:str.utf8,items:list[str.utf8]}fnstash[lent.mut'l':Locker,'item':str.utf8]->boolorfail[full, bad_item: str.utf8]{iflist.length[lend'l'.items]at_least2{failfull;}if'item'equal_tostr.empty{failbad_item["(empty)"];}list.append[lend.mut'l'.items,valuegive'item'];returntrue;}fnparse_digit['t':str.utf8]->int32orfail[not_a_digit]{if't'equal_to"7"{return7;}failnot_a_digit;}fndouble_digit['t':str.utf8]->int32orfail[not_a_digit]{let'd':=parse_digit[copy't']otherwisefail;# pass it upreturn'd'times2;}fnsave[lent'l':Locker]->fail[disk_full]{iflist.length[lend'l'.items]>1{faildisk_full;}}MAIN{let'mine':=Locker[owner="Tankun",items=list.empty];let'names':=["rope",str.empty,"helmet","torch"];for'n'inlend'names'{let'ok':=stash[lend.mut'mine',copy'n']otherwise{full{std::print.stored["full, stopping",newln];break;}bad_item'why'{std::print.stored["bad item ",'why',newln];continue;}};std::print.stored["stored ",'n',newln];}let'a':=double_digit["7"]otherwise0;let'b':=double_digit["x"]otherwise0;std::print.useful['a'," ",'b',newln];save[lend'mine']otherwise{disk_full{std::print.stored["could not save",newln];}};let'c':=parse_digit["x"]otherwisestd::exit[3];std::print.stored["unreachable",newln];}
Output
stored rope
bad item (empty)
stored helmet
full, stopping
14 0
could not save