
On 2021-09-30 5:34 p.m., Barry Scott wrote:
On 30 Sep 2021, at 17:25, Soni L. <fakedme+py@gmail.com <mailto:fakedme+py@gmail.com>> wrote:
Alright, some ppl asked us to rephrase this, so:
The plan is to take the function syntax:
def name(args):
and add an optional "with" to it:
def name(args) with exceptions:
these then get added to the function, similar to e.g. default args. when an exception is thrown*, the VM then checks these and converts relevant exceptions into RuntimeError, e.g.:
def foo(): raise Bar def baz() with Bar: foo() baz()
would make a RuntimeError, because foo raised a Bar and the VM sees that Bar is in baz's with.
Does with Bar mean that Bar is expected?
If so who cares if foo raises it? Are you really saying I cannot call functions to implement a complex algorithm that raises exceptions?
No, we're not saying that, altho it would be more verbose. Consider the example: def foo(): raise Bar def baz() with Bar: try: foo() except Bar: raise baz() This would successfully propagate foo's Bar to baz's caller. That's all - just making the exception propagation points explicit rather than implicit. (heh.)
The caller of baz is expecting Bar right?
*except "raise" opcodes SKIP checking these (within the context of the function), so the following:
def baz() with Bar: raise Bar baz()
raises a Bar, not a RuntimeError from a Bar.
You want to include the exceptions that a function can raise in its signature and have python enforce rules based on that information.
C++ had/has this feature and it failed in practice so it has been deprecated. I'd be surprised that it will be useful in python given this experience in the C++ world.
Unlike checked exceptions, this idea explicitly doesn't affect the caller. Take a look above - note how baz, being defined as "def baz() with Bar:", can still be called as "baz()" without any exception checking around it. We're trying to move away from checked exceptions here, it just happens that using similar syntax is surprisingly convenient. Sadly we do acknowledge the limitation that the best syntax for this is similar to what's used for checked exceptions in other languages. If you have any better syntax suggestions we're open to them tho. (And yeah, we do agree that *checked exceptions*, not to be confused with this idea, are a practical failure.)
Barry