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

Build T::Class[AttachedClass] #62

Closed
DarkDimius opened this issue Jan 16, 2019 · 5 comments · Fixed by #6781
Closed

Build T::Class[AttachedClass] #62

DarkDimius opened this issue Jan 16, 2019 · 5 comments · Fixed by #6781
Labels
enhancement New feature or surprising current feature hard Daunting task, even for Sorbet team members

Comments

@DarkDimius
Copy link
Collaborator

DarkDimius commented Jan 16, 2019

Input

→ View on sorbet.run

# typed: true
extend T::Sig

module M
  def some_instance_method; end
end

class A
  include M
end

T.let(A.new, M)

# There is no type to write on the RHS that's valid
T.let(A, T.class_of(M))

# We'd like to implement something to make this work
# T.let(A, T::Class[M])

# sig {params(klass: T::Class[M]).void}
sig {params(klass: Class).void}
def example1(klass)
  x = klass.new
  T.reveal_type(x)        # ideally: `M`
  x.some_instance_method  # ideally: no error
end

Observed output

editor.rb:24: Revealed type: Object https://srb.help/7014
    24 |  T.reveal_type(x)        # ideally: `M`
          ^^^^^^^^^^^^^^^^
  Got Object originating from:
    editor.rb:23:
    23 |  x = klass.new
              ^^^^^^^^^

editor.rb:25: Method some_instance_method does not exist on Object https://srb.help/7003
    25 |  x.some_instance_method  # ideally: no error
          ^^^^^^^^^^^^^^^^^^^^^^
  Got Object originating from:
    editor.rb:23:
    23 |  x = klass.new
              ^^^^^^^^^
  Autocorrect: Use `-a` to autocorrect
    editor.rb:25: Replace with singleton_method
    25 |  x.some_instance_method  # ideally: no error
            ^^^^^^^^^^^^^^^^^^^^

editor.rb:15: Argument does not have asserted type T.class_of(M) https://srb.help/7007
    15 |T.let(A, T.class_of(M))
        ^^^^^^^^^^^^^^^^^^^^^^^
  Got T.class_of(A) originating from:
    editor.rb:15:
    15 |T.let(A, T.class_of(M))
              ^
Errors: 3

Expected behavior

  • The commented-out T.let holds
  • The revealed type of x is M
  • x.some_instance_method type checks.
@DarkDimius DarkDimius added the hard Daunting task, even for Sorbet team members label Jan 16, 2019
@jez
Copy link
Collaborator

jez commented Apr 5, 2019

ptarjan pushed a commit that referenced this issue Apr 6, 2019
Correctly modeling this return value is blocked by this issue:
#62
@akshay-stripe
Copy link

subscribing. cc @axyjo

@hdoan741
Copy link
Contributor

subscribing

@jez jez added this to the Generics milestone Sep 27, 2019
lgo added a commit that referenced this issue Jul 22, 2020
Since #1855 is now completed (referenced by #62), it should be possible to use `T.self_type` for this method.
@jez jez added the enhancement New feature or surprising current feature label Jul 16, 2021
elliottt pushed a commit that referenced this issue Jul 30, 2021
* Add empty sorbet_globalConstructors function

* Replace all appendToGlobalCtors with sorbet_globalConstructors

* tools/scripts/update_exp_files.sh

* fixup: Use auto

* Fixup
@searls
Copy link

searls commented May 20, 2022

I left this comment yesterday on #3856. Is it a dupe of #62?

(Either way, this capability would be great for my mocking library, Mocktail! Without it there's not much value in typing)

@grncdr
Copy link

grncdr commented May 25, 2022

It's a bit unclear to me from the example given whether this actually covers the same use-case as #3856. Specifically this example does not make any use of type parameters.

Does fixing this issue imply that the following signature will be possible?

sig { type_parameters(:E).param(T::Class[T.type_parameter(:E)]).returns(T.type_parameter(:E)) }

If not, I think #3856 is a separate issue.

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

Successfully merging a pull request may close this issue.

6 participants