Git Product home page Git Product logo

Comments (1)

straight-shoota avatar straight-shoota commented on June 18, 2024 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.