Class: RDL::Type::GenericType

Inherits:
Type
  • Object
show all
Defined in:
lib/rdl/types/generic.rb

Overview

A type that is parameterized on one or more other types. The base type must be a NominalType or self, while the parameters should be strings or symbols

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#canonical, leq, #nil_type?, #optional?, #to_contract, #vararg?

Constructor Details

#initialize(base, *params) ⇒ GenericType

Returns a new instance of GenericType.

Raises:

  • (RuntimeError)


10
11
12
13
14
15
16
# File 'lib/rdl/types/generic.rb', line 10

def initialize(base, *params)
  raise RuntimeError, "Attempt to create generic type with non-type param" unless params.all? { |p| p.is_a? Type }
  raise "base must be NominalType or self, got #{base} of type #{base.class}" unless ((base.instance_of? NominalType) || ((base.instance_of? VarType) && (base.name.to_s == "self")))
  @base = base
  @params = params
  super()
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



7
8
9
# File 'lib/rdl/types/generic.rb', line 7

def base
  @base
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/rdl/types/generic.rb', line 8

def params
  @params
end

Instance Method Details

#<=(other) ⇒ Object



38
39
40
# File 'lib/rdl/types/generic.rb', line 38

def <=(other)
  return Type.leq(self, other)
end

#==(other) ⇒ Object Also known as: eql?

:nodoc:



22
23
24
25
26
# File 'lib/rdl/types/generic.rb', line 22

def ==(other) # :nodoc:
  return false if other.nil?
  other = other.canonical
  return (other.instance_of? GenericType) && (other.base == @base) && (other.params == @params)
end

#hashObject



55
56
57
# File 'lib/rdl/types/generic.rb', line 55

def hash
  (61 + @base.hash) * @params.hash
end

#instantiate(inst) ⇒ Object



51
52
53
# File 'lib/rdl/types/generic.rb', line 51

def instantiate(inst)
  GenericType.new(base.instantiate(inst), *params.map { |t| t.instantiate(inst) })
end

#match(other) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/rdl/types/generic.rb', line 30

def match(other)
  other = other.canonical
  other = other.type if other.instance_of? AnnotatedArgType
  return true if other.instance_of? WildQuery
  return false unless other.instance_of? GenericType
  return @params.length == other.params.length && @params.zip(other.params).all? { |t,o| t.match(o) }
end

#member?(obj, *args) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# File 'lib/rdl/types/generic.rb', line 42

def member?(obj, *args)
  raise "No type parameters defined for #{base.name}" unless RDL::Globals.type_params[base.name]
#      formals = RDL::Globals.type_params[base.name][0]
  t = RDL::Util.rdl_type obj
  return t <= self if t
  return false unless base.member?(obj, *args)
  return true
end

#to_instObject



59
60
61
# File 'lib/rdl/types/generic.rb', line 59

def to_inst
  return RDL::Globals.type_params[base.name][0].zip(@params).to_h
end

#to_sObject



18
19
20
# File 'lib/rdl/types/generic.rb', line 18

def to_s
  "#{@base}<#{@params.map { |t| t.to_s }.join(', ')}>"
end