ruby / ruby/rbs

"singleton(class with this mixin)" is not supported

Open
#1,715 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.2k
Forks
256
Avg merge
6d 17h
Merged PRs (30d)
37

Description

Currently, the usage of singleton seems to be limited to the following usages:

# foo accepts the FooClass as argument, or any subclass of it
# rb: foo(FooClass)
def foo: (singleton(FooClass))

# foo accepts the FooModule module as argument
# rb: foo(FooModule)
def foo: (singleton(FooModule))

# foo accepts instances of a class with the FooModule mixed in, or with its singleton class extended by it
# rb: foo(foo_class_instance)
def foo: (FooModule)

Currently, it seems to not be possible to declare the acceptance of "the class with FooModule mixed in", for functions called with foo(ClassWithFooModule). I've tried declaring. foo: (Object & FooModule), but this is something which the parser does not accept:

 Syntax error: expected a token `pRPAREN`, token=`&` (pAMP) (RBS::ParsingError)

      def foo: (singleton(Object & Job)) -> void

In lack of an alternative way to declare it, I guess this makes it a feature request. I think this is a quite common pattern, for instance in background job frameworks, such as sidekiq or shoryuken, where you can call Sidekiq.enqueue(JobClass) or pass it to push_bulk, and the JobClass only has to include the Sidekiq::Job mixin (shoryuken works similarly), or in middleware chains, which usually contain classes as arguments, and in some cases these must include some some module, or "quack" like some interface (usually via implementing call(this, that, other)).

(the latter point points to also supporting this pattern for interfaces, i.e. singleton(Object & _IFoo))

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file or test is named in the issue. Start by tracing the parser and type representation for singleton(...) and intersections, then compare the existing singleton class and module cases. Done means the proposed syntax parses and correctly expresses a class whose singleton class includes the requested module or interface, with coverage for the examples described.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.