Skip to content

Instantly share code, notes, and snippets.

@hashmal
Last active December 16, 2015 05:59
Show Gist options
  • Save hashmal/5388054 to your computer and use it in GitHub Desktop.
Save hashmal/5388054 to your computer and use it in GitHub Desktop.

Proposal for "try blocks"

To fail gracefully, an operation needs to manipulate a different stack in order to be able to recover from an error (if an error occurs, the stack is in an undetermined state).

A try block can terminate in two ways:

  • The computation succeeded. In this case, its stack contents are inserted in the main stack
  • The computation fails. In this case, its stack is discarded and a special quoted symbol is put on the main stack (let's say this symbol is :$try/failed)

A try block would be called from Shirka this way:

[stack] [operation] try

This does not require any syntactic sugar. It could be used with prefix notation:

-- (prepare a list to use as a stack)

(try)
  [ do-something ]

Implementing a rescue block then becomes easy:

(=> rescue)
  [ => $rescue/op
    >> :$try/failed =
    [ << $rescue/op ] !? ]

Pairing try and rescue becomes surprisingly natural:

(try)
  [ dangerous-stuff ]
(rescue)
  [ cleaning ]

other-operations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment