简体   繁体   中英

Sorbet doesn't report a missing method implementation (compared to an RBI signature), am I doing something wrong here?

I have a file my_class.rb :

# typed: true
class MyClass
  attr_accessor :a, :b

  def initialize(a: 1, b: 2)
    @a = a
    @b = b
  end

  def do_stuff
    puts "#{self.class}: #@a #@b"

    a * b + a - b
  end
end

After installing and initializing Sorbet , I decided to write an RBI signature for this file in sorbet/rbi/my_class.rbi :

# typed: strict

class MyClass
  sig { params(a: Numeric, b: Numeric).void }
  def initialize(a:, b:); end

  sig { returns(Numeric) }
  def a; end

  sig { returns(Numeric) }
  def b; end

  sig { returns(Numeric) }
  def do_stuff; end

  sig { params(x: Numeric).void }
  def adjust(x); end;
end

I deliberately added a signature for an adjust method to see if Sorbet would report on it. But Sorbet doesn't report any errors.

Am I configuring Sorbet levels wrong here, or doing something else wrong?

Thank you.

I'm new to Sorbet, but I think this is because the RBI signatures don't act as an 'interface' in the traditional sense, so may describe methods that don't exist in the implementation.

https://sorbet.org/docs/abstract discusses interfaces and marking a module with interface! , I think that may give what you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM