Class: Yadriggy::CompositeType

Inherits:
Type
  • Object
show all
Defined in:
lib/yadriggy/type.rb

Overview

Parametric types.

Direct Known Subclasses

Yadriggy::C::ArrayType

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • name (Module)

    type name.

  • args (Array<Type>|Type)

    type arguments.



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

#argsArray<Type> (readonly)

Returns type arguments.

Returns:

  • (Array<Type>)

    type arguments.



563
564
565
# File 'lib/yadriggy/type.rb', line 563

def args
  @args
end

#ruby_classModule (readonly)

Returns type name. The value is a Ruby class.

Returns:

  • (Module)

    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.

Parameters:

  • t (Type)

    the other type.

Returns:

  • (Boolean)

    true if ‘self` is equivalent to `t` or a subtype of `t`.



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.

Parameters:

  • t (Type|Module)

    the other object.

Returns:

  • (Boolean)

    true if ‘self` and `t` represent the same type and their type arguments are equivalent.



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_typeObject



612
613
614
# File 'lib/yadriggy/type.rb', line 612

def exact_type
  @ruby_class
end

#first_argType

Returns the first type argument.

Returns:

  • (Type)

    the first type argument.



573
574
575
# File 'lib/yadriggy/type.rb', line 573

def first_arg
  @args[0]
end

#hashObject



588
589
590
# File 'lib/yadriggy/type.rb', line 588

def hash
  @ruby_class.hash + @args.reduce(0) {|h,p| h + p.hash }
end

#nameString

Obtains the name of this type.

Returns:

  • (String)

    the type name.



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

#supertypeRubyClass

Returns the RubyClass for this class.

Returns:



617
618
619
# File 'lib/yadriggy/type.rb', line 617

def supertype
  RubyClass[@ruby_class]
end