Class: Steep::Interface::TypeParam

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/interface/type_param.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, upper_bound:, variance:, unchecked:, location: nil, default_type:) ⇒ TypeParam

Returns a new instance of TypeParam.



11
12
13
14
15
16
17
18
# File 'lib/steep/interface/type_param.rb', line 11

def initialize(name:, upper_bound:, variance:, unchecked:, location: nil, default_type:)
  @name = name
  @upper_bound = upper_bound
  @variance = variance
  @unchecked = unchecked
  @location = location
  @default_type = default_type
end

Instance Attribute Details

#default_typeObject (readonly)

Returns the value of attribute default_type.



9
10
11
# File 'lib/steep/interface/type_param.rb', line 9

def default_type
  @default_type
end

#locationObject (readonly)

Returns the value of attribute location.



8
9
10
# File 'lib/steep/interface/type_param.rb', line 8

def location
  @location
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/steep/interface/type_param.rb', line 4

def name
  @name
end

#uncheckedObject (readonly)

Returns the value of attribute unchecked.



7
8
9
# File 'lib/steep/interface/type_param.rb', line 7

def unchecked
  @unchecked
end

#upper_boundObject (readonly)

Returns the value of attribute upper_bound.



5
6
7
# File 'lib/steep/interface/type_param.rb', line 5

def upper_bound
  @upper_bound
end

#varianceObject (readonly)

Returns the value of attribute variance.



6
7
8
# File 'lib/steep/interface/type_param.rb', line 6

def variance
  @variance
end

Class Method Details

.rename(params, conflicting_names = params.map(&:name), new_names = conflicting_names.map {|n| AST::Types::Var.fresh_name(n) }) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/steep/interface/type_param.rb', line 35

def self.rename(params, conflicting_names = params.map(&:name), new_names = conflicting_names.map {|n| AST::Types::Var.fresh_name(n) })
  unless conflicting_names.empty?
    hash = conflicting_names.zip(new_names).to_h
    new_types = new_names.map {|n| AST::Types::Var.new(name: n) }

    subst = Substitution.build(conflicting_names, new_types)

    [
      params.map do |param|
        if hash.key?(param.name) || param.upper_bound
          TypeParam.new(
            name: hash[param.name] || param.name,
            upper_bound: param.upper_bound&.subst(subst),
            variance: param.variance,
            unchecked: param.unchecked,
            location: param.location,
            default_type: param.default_type&.subst(subst)
          )
        else
          param
        end
      end,
      subst
    ]
  else
    [params, Substitution.empty]
  end
end

Instance Method Details

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



20
21
22
23
24
25
26
27
# File 'lib/steep/interface/type_param.rb', line 20

def ==(other)
  other.is_a?(TypeParam) &&
    other.name == name &&
    other.upper_bound == upper_bound &&
    other.variance == variance &&
    other.unchecked == unchecked &&
    other.default_type == default_type
end

#hashObject



31
32
33
# File 'lib/steep/interface/type_param.rb', line 31

def hash
  name.hash ^ upper_bound.hash ^ variance.hash ^ unchecked.hash ^ default_type.hash
end

#subst(s) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/steep/interface/type_param.rb', line 98

def subst(s)
  if u = upper_bound
    ub = u.subst(s)
  end

  if d = default_type
    dt = d.subst(s)
  end

  if ub || dt
    update(upper_bound: ub, default_type: dt)
  else
    self
  end
end

#to_sObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/steep/interface/type_param.rb', line 64

def to_s
  buf = +""

  if unchecked
    buf << "unchecked "
  end

  case variance
  when :covariant
    buf << "out "
  when :contravariant
    buf << "in "
  end

  buf << name.to_s

  if upper_bound
    buf << " < #{upper_bound}"
  end

  buf
end

#update(name: self.name, upper_bound: self.upper_bound, variance: self.variance, unchecked: self.unchecked, location: self.location, default_type: self.default_type) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/steep/interface/type_param.rb', line 87

def update(name: self.name, upper_bound: self.upper_bound, variance: self.variance, unchecked: self.unchecked, location: self.location, default_type: self.default_type)
  TypeParam.new(
    name: name,
    upper_bound: upper_bound,
    variance: variance,
    unchecked: unchecked,
    location: location,
    default_type: default_type
  )
end