soutaro / rbs-inline

Inline RBS type declaration
MIT License
165 stars 6 forks source link

`@rbs class` syntax #66

Closed soutaro closed 1 week ago

soutaro commented 1 week ago

Some methods allow defining methods to another class inside blocks.

module Foo
  class_methods do
    # Defines `ClassMethods` module ande defines methods in it
    def foo = 123
  end
end

I plan to introduce @rbs class C and @rbs module M commands, that wraps the contents of the block inside a class/module declaration.

module Foo
  class_methods do
    # @rbs module ClassMethods
    def foo = 123
  end
end

(end is missing, but we know it's acceptable from Haml.)

ParadoxV5 commented 1 week ago

Example with just Ruby stdlib:

module Foo
  instance_eval do
    # @rbs module ClassMethods
    def foo = 123
  end
end
soutaro commented 1 week ago

Implemented in #69, with a minor syntax change.

module Foo
  # The @rbs module annotation is given above the block call.

  # @rbs module ClassMethods
  class_methods do
    def foo = 123
  end
end
soutaro commented 1 week ago

@ParadoxV5 By the way, the @rbs module syntax would not be sufficient for the instance_eval example.

What we need is to do it in singleton class context. Not defining another module for the block.

module Foo
  # We don't want ClassMethods module...
  module ClassMethods
    def foo: () -> untyped
  end

  def self.foo: () -> untyped    # This is expected
end

Maybe we need @rbs class <<self???

ParadoxV5 commented 1 week ago

We could just not support it. Is there a concrete reason one’d use instance_eval do over class << self in Ruby?