Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behaviour of argless Proc pointers from methods #10728

Open
HertzDevil opened this issue May 20, 2021 · 0 comments
Open

Inconsistent behaviour of argless Proc pointers from methods #10728

HertzDevil opened this issue May 20, 2021 · 0 comments

Comments

@HertzDevil
Copy link
Contributor

The ->foo literal means two different things depending on where it is applied:

  • As an argument to a lib fun, the resulting Proc takes the same arguments as that of the corresponding fun parameter:
    lib L
      fun foo(x : Int32 ->)
    end
    
    def bar(x = 0)
    end
    
    L.foo(->bar) # expands to `->(arg0 : Int32) { bar(arg0) }`
  • As an argument to regular defs, the resulting Proc takes no arguments:
    module L
      def self.foo(x : ->)
      end
    
      def self.foo(x : Int32 ->)
      end
    end
    
    def bar(x = 0)
    end
    
    L.foo(->bar) # expands to `-> { bar }`, calls the first overload

This situation is somewhat confusing, so I tend to always specify the argument types to those pointers. But it turns out explicitly providing parentheses to argless pointers is a syntax error:

def bar(x = 0)
end

->bar() # Error: unexpected token: )

The above should be allowed, similar to how super and previous_def accept empty parentheses. (See also #10204.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants