notes-computer-jasper-jasperError

Maybe/Option towers, with names (analogy with keyword args?)

syntactically concise way to transmute exceptions into Nothings, and vice versa

syntactically concise way to transmute exceptions into error codes, and vice versa

syntactically concise way to transmute exceptions into sentinel values of your choice, and vice versa

syntactically concise ways to ignore exceptions

uniform severity scheme in the exception hierarchy

a way to layer exceptions, e.g. if you catch an exception and then throw your own exception as a result, the new exception should have a pointer to the original exception within it

Design todos

Methods like RAII, which C++ programmers love to mention as the ultimate solution to this problem, go a long way to protect against this. But they aren't a silver bullet. It will make sure you release resources on a throw, but doesn't free you from having to think about corruption of object state and callers seeing intermediate values. So, for a lot of people, it's easier to say, by fiat of coding style, no exceptions. If you restrict the kind of code you write, it's harder to introduce these bugs. If you don't, it's fairly easy to make a mistake."

Design toreads


i think the way to go is Haskell Maybe types/Scala Option types.

In Haskell, you can have e.g. a type 'Int', which is non-nullable and can have values like 3, and then you can have type 'Maybe Int', which can have values like 'Just 3', 'None'. Then you can layer these; you can have a type 'Maybe Maybe Int', which can have value like 'Just Just 3', 'Just None', 'None'.

The main problem with the Haskell way of doing it is that the syntax is too verbose because it's a pain to type Just and None all the time; you are always having to use pattern matching/destructuring bind to get the real value out; e.g. in Haskell if you are giving a variable 'x' of type Maybe Int and want to double it, you gotta do something like:

x :: Maybe Int f Just x = x * 2 f None = None

now call f with input '3' y = f (Just 3)

An alternative is to work within the Maybe monad, but that's too verbose for me too.

I am planning to add some syntactic sugar, using the apostrophe character. Not quite sure how it will work, probably like this:

x :: 'Int' f x = x' * 2 like working within the Maybe monad

y = f '3'

the apostrophes would also be used to convert between functions that throw exceptions when they are unhappy, and functions which return null when they are unhappy:

def f(x): if x < 0: raise ImUnhappy? else return x * 2

y :: 'Int' y = f' -3 no exception is thrown, but y is null

same but restrict the apostrophe exception catching to exceptions of type ImUnhappy?:

y :: 'Int' y = f'ImUnhappy? -3

still to be designed is a way to name the 'layers' of a nested Maybe and to have an apostrophe before a word address these layers.

Doug: > I'm not sure the apostrophe suffices > for functions with multiple parameters. Surely there should be a way to > lift some of the arguments without lifting others?

actually we're lifting the return arguments not the input arguments.

but still, there are multiple return arguments in Jasper, so your point stands. Mb the apostrophe should be applied to the lvalue:

y' = f -3

---

in Jasper, i was thinking that this function can be written without any error handling:

int function(int first, int second) { return first * second; }

and then it can be called as:

y = function(3, 4)

resulting in y == 12

or as

y' = function(3, nil)

resulting in y == nil

or as

'y = function(3, nil)

resulting in a runtime exception being thrown

passing in a nil without any error handling is a compile-time error:

y = function(3, nil) compile-time error

only if the function wants to do some complicated error handing itself, would it need to use apostrophes in its types:

'int' function2('int' first, 'int' second) { if first == nil or second == nil: return nil

       return first * second;}

or

int function3('int' first, 'int' second) { if first == nil or second == nil: raise ImUnhappy?

       return first * second;}

note that an apostrophe on the right side converts an exception to a nil, e.g.

y' = function3(3, nil)

results in y == nil, not in an exception.

whereas 'y does the converse, e.g.

'y = function2(3, nil)

results in a runtime exception.

so the apostrophes are operators. y' means "evaluate the expression in the rvalue and if during evaluation, a nil pops up where it shouldn't be, just propagate the nil and if as a result y is assigned a nil, no problem; and if an exception is raised and reaches this point, ignore it and just return a nil".

'y means "evaluate the expression and if a nil is assigned to y, then raise an exception"

---