Class: TypedRb::Model::TmMlhs

Inherits:
Expr show all
Defined in:
lib/typed/model/tm_mlhs.rb

Overview

Deconstructing arg variables

Instance Attribute Summary collapse

Attributes inherited from Expr

#col, #line, #node, #type

Instance Method Summary collapse

Constructor Details

#initialize(args, node) ⇒ TmMlhs

Returns a new instance of TmMlhs.



9
10
11
12
# File 'lib/typed/model/tm_mlhs.rb', line 9

def initialize(args, node)
  super(node)
  @args = args
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



8
9
10
# File 'lib/typed/model/tm_mlhs.rb', line 8

def args
  @args
end

#lambda_argsObject

Returns the value of attribute lambda_args.



8
9
10
# File 'lib/typed/model/tm_mlhs.rb', line 8

def lambda_args
  @lambda_args
end

Instance Method Details

#check_type(actual_argument, context) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/typed/model/tm_mlhs.rb', line 14

def check_type(actual_argument, context)
  return process_lambda_args(context) if actual_argument == :lambda
  if pair_argument?(actual_argument)
    process_pair(actual_argument, context)
  elsif array_argument?(actual_argument)
    process_array(actual_argument, context)
  else
    fail TypeCheckError.new("Error type checking function MLHS term: Type is not subtype of Array:  #{actual_argument}", node)
  end
end

#compatible?(other_type, relation = :lt) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/typed/model/tm_mlhs.rb', line 25

def compatible?(other_type, relation = :lt)
  if other_type.generic? && other_type.ruby_type.ancestors.include?(Array)
    if other_type.type_vars.size == 1
      @lambda_args.each do |lambda_arg|
        lambda_arg.compatible?(other_type.type_vars.first, relation)
      end
    elsif other_type.type_vars.size == @lambda_args.size
      @lambda_args.each_with_index do |lambda_arg, i|
        lambda_arg.compatible?(other_type.type_vars[i], relation)
      end
    else
      false
    end
  else
    false
  end
end