How to type a subclass of DelegateClass with Sorbet?

216 Views Asked by At

I want to type a class which inherits DelegateClass(SomeClass). The method Kernel#DelegateClass receives a class, and returns a new class which delegates methods to the given class instance (docs).

Example (playground):

class ExtArray < DelegateClass(Array)
  def find_by_some_complex_condition
    find { |element| some_complex_condition(element) }
  end
end

First, Sorbet does not allow a superclass to be non-constant (https://srb.help/4002). So the first idea I came up with is using an alias.

First Try (playground):

DelegatedArray = DelegateClass(Array)

class ExtArray < DelegatedArray
  def find_by_some_complex_condition
    find { |element| some_complex_condition(element) }
  end
end

But this is not type-checked too, because Sorbet disallows a superclass to be a class alias (https://srb.help/7003).

Then, how should I rewrite codes to be type-checked by Sorbet? It's OK to rewrite codes totally and remove DelegateClass, but delegations are necessary.

Environment

  • Ruby 3.0.5
  • Sorbet 0.5.10751
  • Tapioca 0.11.4
  • macOS Ventura 13.3.1
0

There are 0 best solutions below