Class: RBS::AST::TypeParam

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/ast/type_param.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of TypeParam.



8
9
10
11
12
13
14
# File 'lib/rbs/ast/type_param.rb', line 8

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

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



6
7
8
# File 'lib/rbs/ast/type_param.rb', line 6

def location
  @location
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/rbs/ast/type_param.rb', line 6

def name
  @name
end

#upper_boundObject (readonly)

Returns the value of attribute upper_bound.



6
7
8
# File 'lib/rbs/ast/type_param.rb', line 6

def upper_bound
  @upper_bound
end

#varianceObject (readonly)

Returns the value of attribute variance.



6
7
8
# File 'lib/rbs/ast/type_param.rb', line 6

def variance
  @variance
end

Class Method Details

.rename(params, new_names:) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rbs/ast/type_param.rb', line 93

def self.rename(params, new_names:)
  raise unless params.size == new_names.size

  subst = Substitution.build(new_names, Types::Variable.build(new_names))

  params.map.with_index do |param, index|
    new_name = new_names[index]

    TypeParam.new(
      name: new_name,
      variance: param.variance,
      upper_bound: param.upper_bound&.map_type {|type| type.sub(subst) },
      location: param.location
    ).unchecked!(param.unchecked?)
  end
end

.resolve_variables(params) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/rbs/ast/type_param.rb', line 71

def self.resolve_variables(params)
  return if params.empty?

  vars = Set.new(params.map(&:name))

  params.map! do |param|
    param.map_type {|bound| _ = subst_var(vars, bound) }
  end
end

.subst_var(vars, type) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rbs/ast/type_param.rb', line 81

def self.subst_var(vars, type)
  case type
  when Types::ClassInstance
    namespace = type.name.namespace
    if namespace.relative? && namespace.empty? && vars.member?(type.name.name)
      return Types::Variable.new(name: type.name.name, location: type.location)
    end
  end

  type.map_type {|t| subst_var(vars, t) }
end

Instance Method Details

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



25
26
27
28
29
30
31
# File 'lib/rbs/ast/type_param.rb', line 25

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

#hashObject



35
36
37
# File 'lib/rbs/ast/type_param.rb', line 35

def hash
  self.class.hash ^ name.hash ^ variance.hash ^ upper_bound.hash ^ unchecked?.hash
end

#map_type(&block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rbs/ast/type_param.rb', line 58

def map_type(&block)
  if b = upper_bound
    _upper_bound = yield(b)
  end

  TypeParam.new(
    name: name,
    variance: variance,
    upper_bound: _upper_bound,
    location: location
  ).unchecked!(unchecked?)
end

#rename(name) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/rbs/ast/type_param.rb', line 49

def rename(name)
  TypeParam.new(
    name: name,
    variance: variance,
    upper_bound: upper_bound,
    location: location
  ).unchecked!(unchecked?)
end

#to_json(state = JSON::State.new) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/rbs/ast/type_param.rb', line 39

def to_json(state = JSON::State.new)
  {
    name: name,
    variance: variance,
    unchecked: unchecked?,
    location: location,
    upper_bound: upper_bound
  }.to_json(state)
end

#to_sObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rbs/ast/type_param.rb', line 110

def to_s
  s = +""

  if unchecked?
    s << "unchecked "
  end

  case variance
  when :invariant
    # nop
  when :covariant
    s << "out "
  when :contravariant
    s << "in "
  end

  s << name.to_s

  if type = upper_bound
    s << " < #{type}"
  end

  s
end

#unchecked!(value = true) ⇒ Object



16
17
18
19
# File 'lib/rbs/ast/type_param.rb', line 16

def unchecked!(value = true)
  @unchecked = value ? true : false
  self
end

#unchecked?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/rbs/ast/type_param.rb', line 21

def unchecked?
  @unchecked
end