Comments (1)
This seems very reasonable and I wouldn't have been surprised if it already worked this way. It's even a bit unexpected that it doesn't. Esepcially since it's supported in Ruby. So might just be an unintentional omission.
In my mental model, rescue ex : Type
is equivalent to an elective type restriction with re-raising on non-match.
rescue ex : Foo
bar
end
This is more convenient to write than the long form:
rescue ex
if ex.is_a?(Foo)
bar
else
raise ex
end
end
The actual type casting seems to work fine with module types. It's just the extra validation that checks for being a descendent of ::Exception
which makes it impossible to filter for modules.
So I think the implementation could be pretty trivial:
--- i/src/compiler/crystal/semantic/main_visitor.cr
+++ w/src/compiler/crystal/semantic/main_visitor.cr
@@ -2775,7 +2775,7 @@ module Crystal
types = node_types.map do |type|
type.accept self
instance_type = type.type.instance_type
- unless instance_type.implements?(@program.exception)
+ unless instance_type.implements?(@program.exception) || instance_type.module?
type.raise "#{instance_type} is not a subclass of Exception"
end
instance_type
from crystal.
Related Issues (20)
- UTF-16 string literals HOT 14
- Non-atomic memory is cleared twice HOT 6
- `GC.malloc` doesn't clear memory (as specified) for `gc_none`
- [RFC] Add `URI::Params::Serializable`
- UTF-8 support in Windows API HOT 1
- Non-zero padded exponent in float string representation after Ryu implementation HOT 6
- Allocation of non-zeroed memory for performance boost HOT 11
- Crystal::Macros::Arg#restriction method always returns Nop HOT 1
- Nilable `Proc` types inside libs
- Cannot return `Proc`s from top-level funs
- `ReferenceStorage(T)` is always atomic even when `T` isn't HOT 1
- Add `crystal tool method_types` for listing method parameter types HOT 4
- Passing nil to Addrinfo.getaddrinfo gives unexpected error message HOT 1
- Package installation fails on Windows due to missing SQLite3 .lib files HOT 2
- `File#truncate` raises `File::AccessDeniedError` on Windows when file was opened in append mode HOT 3
- Cache compiler results for tools
- Include more types in `crystal tool hierarchy` HOT 9
- `close_on_exec` on Windows HOT 2
- Pointer equality for `Slice` HOT 2
- Forbid variable assignment in function call HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crystal.