Skip to content

Allow abstract methods to be implemented via method_missing #11

@amomchilov

Description

@amomchilov

In theory: this should be possible:

# typed: true

require "type_toolkit"

module MyInterface
  interface!
  
  #: -> void
  def demo; end
end

class Parent
  def method_missing(m, ...)
    return super unless m == :demo
    
    puts "#demo implemented via #method_missing"
  end
end

class Child < Parent
  # Commenting out this module inclusion makes call succeed
  include MyInterface
end

Child.new.demo

For comparison, Sorbet's static typechecker doesn't allow this, because it doesn't have any way to prove what is or isn't implemented by method_missing. Sorbet runtime raises an error, which can be a change in runtime behaviour (compared to what you would expect without it):

# typed: true

require "sorbet-runtime"

module MyInterface
  extend T::Sig
  extend T::Helpers
  
  interface!
  
  sig { abstract.void }
  def demo; end
end

class Parent
  def method_missing(m, ...)
    return super unless m == :demo
    
    puts "#demo implemented via #method_missing"
  end
end

class Child < Parent
  # Commenting out this module inclusion makes call succeed
  include MyInterface
end

Child.new.demo # (NotImplementedError)
# The method `demo` on MyInterface is declared as `abstract`. It does not have an implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions