sirisian / ecmascript-types

ECMAScript Optional Static Typing Proposal http://sirisian.github.io/ecmascript-types/
453 stars 4 forks source link

Typed Exceptions and Exception Filters #22

Closed sirisian closed 6 years ago

sirisian commented 7 years ago

Analyze the syntax choices with typed exceptions and exception filters. Find the strengths and weaknesses of both and whether they conflict in a breaking way that prohibits adding exception filters at a later time outside of this specification.

Refer to C#: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch

A simple typed exception is:

try
{
    // Code
}
catch (e:TypeError)
{
}

In C# they use a when keyword to add filtering. If someone wants to breakdown what every other language allows or alternatives this would help a lot. It's not important which syntax is chosen, but what is important is ensuring that this specification doesn't conflict with future specifications.

For example the following syntax can be added later without conflicting:

try
{
    // Code
}
catch (e:TypeError) when (e.example == `foo`)
{
}

This introduces a new keyword though and it's possible future proposals with new keywords would be scoffed at for adding complexity. I'm not even sure TC39 would agree to adding something that could be written with an if statement so exception filters if they are implemented would probably have to have clear strengths.

At the very least I want to include a future consideration section with various proposed exception filtering proposals to show it doesn't conflict. I've had two people mention them off hand, but I haven't gotten any clarification to what their worry was. It might seem naive, but the goal is that anyone can read the spec and feel confident that their worries have been addressed.

Possible syntax:

catch (e:TypeError, e => e.example == `foo`)

Lastly weird syntax, and not that much more compact:

catch (e:TypeError => e.example == `foo`)

This last one is weird because the catch would be expected to extract the first parameter from the arrow function. These have the advantage though that they don't introduce a new keyword which makes them more likely to be considered though.

sirisian commented 6 years ago

Added a stub for future considerations.

https://github.com/sirisian/ecmascript-types#exception-filters