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

Better return type inference for proc and lambda values #4149

Open
jez opened this issue Apr 15, 2021 · 0 comments
Open

Better return type inference for proc and lambda values #4149

jez opened this issue Apr 15, 2021 · 0 comments
Labels
enhancement New feature or surprising current feature hard Daunting task, even for Sorbet team members
Milestone

Comments

@jez
Copy link
Collaborator

jez commented Apr 15, 2021

Input

→ View on sorbet.run

# typed: true
extend T::Sig

f = proc {|x| 2}
T.reveal_type(f)

g = -> (x) {2}
T.reveal_type(g)

h = Proc.new {|x| 2}
T.reveal_type(h)

i = lambda {|x| 2}
T.reveal_type(i)

Observed output

editor.rb:5: Revealed type: T.proc.params(arg0: T.untyped).returns(T.untyped) https://srb.help/7014
     5 |T.reveal_type(f)
        ^^^^^^^^^^^^^^^^
  Got T.proc.params(arg0: T.untyped).returns(T.untyped) originating from:
    editor.rb:4:
     4 |f = proc {|x| 2}
            ^^^^^^^^^^^^

editor.rb:8: Revealed type: T.proc.params(arg0: T.untyped).returns(T.untyped) https://srb.help/7014
     8 |T.reveal_type(g)
        ^^^^^^^^^^^^^^^^
  Got T.proc.params(arg0: T.untyped).returns(T.untyped) originating from:
    editor.rb:7:
     7 |g = -> (x) {2}
            ^^^^^^^^^^

editor.rb:11: Revealed type: T.untyped https://srb.help/7014
    11 |T.reveal_type(h)
        ^^^^^^^^^^^^^^^^
  Got T.untyped originating from:
    editor.rb:10:
    10 |h = Proc.new {|x| 2}
            ^^^^^^^^^^^^^^^^

editor.rb:14: Revealed type: T.proc.params(arg0: T.untyped).returns(T.untyped) https://srb.help/7014
    14 |T.reveal_type(i)
        ^^^^^^^^^^^^^^^^
  Got T.proc.params(arg0: T.untyped).returns(T.untyped) originating from:
    editor.rb:13:
    13 |i = lambda {|x| 2}
            ^^^^^^^^^^^^^^
Errors: 4

Expected behavior

  • It's very unexpected that Proc.new returns T.untyped

  • It's somewhat unexpected that the inferred return types are T.untyped

  • It's actually expected that the arguments don't have an inferred type. Sorbet makes no effort to infer arguments (this is true for methods too). We might want to expose a way to annotate argument types for proc/lambda values, but that is out of scope for this issue.


@jez jez added bug Something isn't working hard Daunting task, even for Sorbet team members labels Apr 15, 2021
@jez jez added this to the Generics milestone Jul 23, 2022
@jez jez added enhancement New feature or surprising current feature and removed bug Something isn't working labels Aug 14, 2022
jez added a commit that referenced this issue Mar 1, 2024
Partial fix for #3914
Partial fix for #4149
@jez jez mentioned this issue Mar 1, 2024
1 task
jez added a commit that referenced this issue Mar 1, 2024
Partial fix for #3914
Partial fix for #4149
jez added a commit that referenced this issue Apr 25, 2024
Sometimes it's useful to be able to use the arity of the block to guess
an overload.

This isn't perfect for all the reasons that overload checking isn't
perfect, but there are some places where this is useful, especially in
abstractions that check the proc's arity when deciding how to call the
block.

This is also a pre-requisite for doing something like #7741, which is a
partial fix for #3914 / #4149, where we infer the types of
`Kernel#lambda` blocks by codegenerating overloaded signatures.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or surprising current feature hard Daunting task, even for Sorbet team members
Projects
None yet
Development

No branches or pull requests

1 participant