Module: RubyBreaker::Typing

Includes:
TypeDefs
Defined in:
lib/rubybreaker/typing/subtyping.rb

Overview

This module contains subtyping logic used in RubyBreaker. See rubytype.rb for logic related to subclassing which is directly related to pure Ruby types.

Class Method Summary collapse

Class Method Details

.subtype_rel?(lhs, rhs) ⇒ Boolean

This method determines if one type is a subtype of another. This check is for RubyBreaker defined types. See TypeDefs module for more detail.

lhs

The allegedly “subtype”

rhs

The allegedly “supertype”

Returns:

  • (Boolean)


441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/rubybreaker/typing/subtyping.rb', line 441

def self.subtype_rel?(lhs, rhs)

  # Don't even bother if they are same object or syntactically
  # equivalent. NOTE: would this really help improve performance???
  return true if (lhs.equal?(rhs) || lhs.eql?(rhs))

  # Break down the cases by what LHS is.
  is_subtype = false
  if lhs.instance_of?(NilType)
    is_subtype = rhs.instance_of?(NilType)
  elsif lhs.instance_of?(AnyType)
    is_subtype = true 
  elsif lhs.instance_of?(SelfType)
    is_subtype = self.self_subtype_rel?(lhs,rhs)
  elsif lhs.instance_of?(NominalType)
    is_subtype = self.nominal_subtype_rel?(lhs,rhs)
  elsif lhs.instance_of?(FusionType)
    is_subtype = self.fusion_subtype_rel?(lhs,rhs)
  elsif lhs.instance_of?(DuckType)
    is_subtype = self.duck_subtype_rel?(lhs,rhs)
  elsif lhs.instance_of?(MethodType)
    is_subtype = self.method_subtype_rel?(lhs,rhs)
  elsif lhs.instance_of?(OrType)
    is_subtype = self.or_subtype_rel?(lhs,rhs)
  elsif lhs.instance_of?(BlockType)
    is_subtype = self.proc_subtype_rel?(lhs,rhs)
  elsif lhs.instance_of?(MethodListType)
    is_subtype = self.method_subtype_rel?(lhs,rhs)
  end
  return is_subtype
end