Class: Yadriggy::CompositeType
Overview
Parametric types.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#args ⇒ Array<Type>
readonly
Type arguments.
-
#ruby_class ⇒ Module
readonly
Type name.
Instance Method Summary collapse
-
#<=(t) ⇒ Boolean
Check the subtype relation.
-
#==(t) ⇒ Boolean
Checks the equality.
- #exact_type ⇒ Object
-
#first_arg ⇒ Type
The first type argument.
- #hash ⇒ Object
-
#initialize(name, args) ⇒ CompositeType
constructor
A new instance of CompositeType.
-
#name ⇒ String
Obtains the name of this type.
-
#supertype ⇒ RubyClass
The RubyClass for this class.
Methods inherited from Type
#!=, #copy, #eql?, error_found!, get_instance_method_object, #get_method_object, #has_role?, #is_super_of?, role
Constructor Details
#initialize(name, args) ⇒ CompositeType
Returns a new instance of CompositeType.
567 568 569 570 |
# File 'lib/yadriggy/type.rb', line 567 def initialize(name, args) @ruby_class = name @args = args.is_a?(Array) ? args : [ args ] end |
Instance Attribute Details
#args ⇒ Array<Type> (readonly)
Returns type arguments.
563 564 565 |
# File 'lib/yadriggy/type.rb', line 563 def args @args end |
#ruby_class ⇒ Module (readonly)
Returns type name. The value is a Ruby class.
561 562 563 |
# File 'lib/yadriggy/type.rb', line 561 def ruby_class @ruby_class end |
Instance Method Details
#<=(t) ⇒ Boolean
Check the subtype relation.
597 598 599 600 601 602 603 604 605 606 607 608 609 |
# File 'lib/yadriggy/type.rb', line 597 def <= (t) if t.is_super_of?(self) true else ct = CompositeType.role(t) if ct.nil? RubyClass[@ruby_class] <= t else ct.ruby_class == @ruby_class && @args.zip(ct.args).all? {|tt| tt[0] <= tt[1] } end end end |
#==(t) ⇒ Boolean
Checks the equality.
581 582 583 584 585 |
# File 'lib/yadriggy/type.rb', line 581 def == (t) ct = CompositeType.role(t) !ct.nil? && ct.ruby_class == @ruby_class && ct.args == @args end |
#exact_type ⇒ Object
612 613 614 |
# File 'lib/yadriggy/type.rb', line 612 def exact_type @ruby_class end |
#first_arg ⇒ Type
Returns the first type argument.
573 574 575 |
# File 'lib/yadriggy/type.rb', line 573 def first_arg @args[0] end |
#hash ⇒ Object
588 589 590 |
# File 'lib/yadriggy/type.rb', line 588 def hash @ruby_class.hash + @args.reduce(0) {|h,p| h + p.hash } end |
#name ⇒ String
Obtains the name of this type.
623 624 625 626 627 |
# File 'lib/yadriggy/type.rb', line 623 def name name = @ruby_class.name name << '<' << @args.map{|e| e.name }.join(',') << '>' name end |