Class: RBS::VarianceCalculator
- Inherits:
-
Object
- Object
- RBS::VarianceCalculator
- Defined in:
- lib/rbs/variance_calculator.rb
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
Instance Method Summary collapse
- #env ⇒ Object
- #in_inherit(name:, args:, variables:) ⇒ Object
- #in_method_type(method_type:, variables:) ⇒ Object
-
#initialize(builder:) ⇒ VarianceCalculator
constructor
A new instance of VarianceCalculator.
- #type(type, result:, context:) ⇒ Object
Constructor Details
#initialize(builder:) ⇒ VarianceCalculator
Returns a new instance of VarianceCalculator.
61 62 63 |
# File 'lib/rbs/variance_calculator.rb', line 61 def initialize(builder:) @builder = builder end |
Instance Attribute Details
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
59 60 61 |
# File 'lib/rbs/variance_calculator.rb', line 59 def builder @builder end |
Instance Method Details
#env ⇒ Object
65 66 67 |
# File 'lib/rbs/variance_calculator.rb', line 65 def env builder.env end |
#in_inherit(name:, args:, variables:) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/rbs/variance_calculator.rb', line 88 def in_inherit(name:, args:, variables:) type = Types::ClassInstance.new(name: name, args: args, location: nil) Result.new(variables: variables).tap do |result| type(type, result: result, context: :covariant) end end |
#in_method_type(method_type:, variables:) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rbs/variance_calculator.rb', line 69 def in_method_type(method_type:, variables:) result = Result.new(variables: variables) method_type.type.each_param do |param| type(param.type, result: result, context: :contravariant) end if method_type.block method_type.block.type.each_param do |param| type(param.type, result: result, context: :covariant) end type(method_type.block.type.return_type, result: result, context: :contravariant) end type(method_type.type.return_type, result: result, context: :covariant) result end |
#type(type, result:, context:) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rbs/variance_calculator.rb', line 96 def type(type, result:, context:) case type when Types::Variable if result.include?(type.name) case context when :covariant result.covariant(type.name) when :contravariant result.contravariant(type.name) when :invariant result.invariant(type.name) end end when Types::ClassInstance, Types::Interface decl = env.find_class(type.name) type.args.each.with_index do |ty, i| var = decl.type_params.params[i] case var.variance when :invariant type(ty, result: result, context: :invariant) when :covariant type(ty, result: result, context: context) when :contravariant con = case context when :invariant :invariant when :covariant :contravariant when :contravariant :covariant end type(ty, result: result, context: con) end end when Types::Tuple, Types::Record, Types::Union, Types::Intersection # Covariant types type.each_type do |ty| type(ty, result: result, context: context) end end end |