Class: Yoda::Model::TypeExpressions::FunctionType

Inherits:
Base
  • Object
show all
Defined in:
lib/yoda/model/type_expressions/function_type.rb,
lib/yoda/model/type_expressions/function_type/parameter.rb

Defined Under Namespace

Classes: Parameter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==

Constructor Details

#initialize(context: nil, return_type:, required_parameters: [], optional_parameters: [], rest_parameter: nil, post_parameters: [], optional_keyword_parameters: [], required_keyword_parameters: [], keyword_rest_parameter: nil, block_parameter: nil) ⇒ FunctionType

Returns a new instance of FunctionType.

Parameters:

  • context (Base, nil) (defaults to: nil)
  • required_parameters (Array<Base>) (defaults to: [])
  • optional_parameters (Array<Base>) (defaults to: [])
  • rest_parameter (Base, nil) (defaults to: nil)
  • post_parameters (Array<Base>) (defaults to: [])
  • required_keyword_parameters (Array<(String, Base)>) (defaults to: [])
  • optional_keyword_parameters (Array<(String, Base)>) (defaults to: [])
  • keyword_rest_parameter (Base, nil) (defaults to: nil)
  • block_parameter (Base, nil) (defaults to: nil)
  • return_type (Base)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/yoda/model/type_expressions/function_type.rb', line 35

def initialize(context: nil, return_type:, required_parameters: [], optional_parameters: [], rest_parameter: nil, post_parameters: [], optional_keyword_parameters: [], required_keyword_parameters: [], keyword_rest_parameter: nil, block_parameter: nil)
  fail TypeError, return_type unless return_type.is_a?(Base)
  fail TypeError, context if context && !context.is_a?(Parameter)
  fail TypeError, rest_parameter if rest_parameter && !rest_parameter.is_a?(Parameter)
  fail TypeError, keyword_rest_parameter if keyword_rest_parameter && !keyword_rest_parameter.is_a?(Parameter)
  fail TypeError, block_parameter if block_parameter && !block_parameter.is_a?(Parameter)
  fail TypeError, required_parameters unless required_parameters.all? { |param| param.is_a?(Parameter) }
  fail TypeError, optional_parameters unless optional_parameters.all? { |param| param.is_a?(Parameter) }
  fail TypeError, post_parameters unless post_parameters.all? { |param| param.is_a?(Parameter) }
  fail TypeError, optional_keyword_parameters unless optional_keyword_parameters.all? { |param| param.is_a?(Parameter) }
  fail TypeError, required_keyword_parameters unless required_keyword_parameters.all? { |param| param.is_a?(Parameter) }

  @context = context
  @required_parameters = required_parameters
  @optional_parameters = optional_parameters
  @required_keyword_parameters = required_keyword_parameters
  @optional_keyword_parameters = optional_keyword_parameters
  @rest_parameter = rest_parameter
  @post_parameters = post_parameters
  @keyword_rest_parameter = keyword_rest_parameter
  @block_parameter = block_parameter
  @return_type = return_type
end

Instance Attribute Details

#block_parameterParameter? (readonly)

Returns:



20
21
22
# File 'lib/yoda/model/type_expressions/function_type.rb', line 20

def block_parameter
  @block_parameter
end

#contextBase? (readonly)

Returns:



8
9
10
# File 'lib/yoda/model/type_expressions/function_type.rb', line 8

def context
  @context
end

#keyword_rest_parameterParameter? (readonly)

Returns:



20
21
22
# File 'lib/yoda/model/type_expressions/function_type.rb', line 20

def keyword_rest_parameter
  @keyword_rest_parameter
end

#optional_keyword_parametersArray<Parameter> (readonly)

Returns:



17
18
19
# File 'lib/yoda/model/type_expressions/function_type.rb', line 17

def optional_keyword_parameters
  @optional_keyword_parameters
end

#optional_parametersArray<Parameter> (readonly)

Returns:



11
12
13
# File 'lib/yoda/model/type_expressions/function_type.rb', line 11

def optional_parameters
  @optional_parameters
end

#post_parametersArray<Parameter> (readonly)

Returns:



11
12
13
# File 'lib/yoda/model/type_expressions/function_type.rb', line 11

def post_parameters
  @post_parameters
end

#required_keyword_parametersArray<Parameter> (readonly)

Returns:



14
15
16
# File 'lib/yoda/model/type_expressions/function_type.rb', line 14

def required_keyword_parameters
  @required_keyword_parameters
end

#required_parametersArray<Parameter> (readonly)

Returns:



11
12
13
# File 'lib/yoda/model/type_expressions/function_type.rb', line 11

def required_parameters
  @required_parameters
end

#rest_parameterParameter? (readonly)

Returns:



20
21
22
# File 'lib/yoda/model/type_expressions/function_type.rb', line 20

def rest_parameter
  @rest_parameter
end

#return_typeBase (readonly)

Returns:



23
24
25
# File 'lib/yoda/model/type_expressions/function_type.rb', line 23

def return_type
  @return_type
end

Instance Method Details

#change_root(namespace) ⇒ FunctionType

Parameters:

Returns:



91
92
93
# File 'lib/yoda/model/type_expressions/function_type.rb', line 91

def change_root(namespace)
  map { |type| type.change_root(namespace) }
end

#eql?(another) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/yoda/model/type_expressions/function_type.rb', line 59

def eql?(another)
  another.is_a?(FunctionType) &&
  context == another.context &&
  required_parameters == another.required_parameters &&
  optional_parameters == another.optional_parameters &&
  required_keyword_parameters.to_set == another.required_keyword_parameters.to_set &&
  optional_keyword_parameters.to_set == another.optional_keyword_parameters.to_set &&
  keyword_rest_parameter == another.keyword_rest_parameter &&
  rest_parameter == another.rest_parameter &&
  post_parameters == another.post_parameters &&
  block_parameter == another.block_parameter &&
  return_type == another.return_type
end

#hashObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/yoda/model/type_expressions/function_type.rb', line 73

def hash
  [
    self.class.name,
    context,
    return_type,
    required_parameters,
    optional_parameters,
    rest_parameter,
    post_parameters,
    required_keyword_parameters,
    optional_keyword_parameters,
    keyword_rest_parameter,
    block_parameter,
  ].hash
end

#map(&block) ⇒ self

Returns:

  • (self)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/yoda/model/type_expressions/function_type.rb', line 131

def map(&block)
  call_map_type = -> (param) { param&.map_type(&block) }

  self.class.new(
    context: context&.map(&block),
    return_type: return_type.map(&block),
    required_parameters: required_parameters.map(&call_map_type),
    optional_parameters: optional_parameters.map(&call_map_type),
    rest_parameter: call_map_type.call(rest_parameter),
    post_parameters: post_parameters.map(&call_map_type),
    required_keyword_parameters: required_keyword_parameters.map(&call_map_type),
    optional_keyword_parameters: required_keyword_parameters.map(&call_map_type),
    keyword_rest_parameter: call_map_type.call(keyword_rest_parameter),
    block_parameter: call_map_type.call(block_parameter),
  )
end

#method_type_signatureObject



101
102
103
104
# File 'lib/yoda/model/type_expressions/function_type.rb', line 101

def method_type_signature
  params_str = all_parameters_to_s
  (params_str.empty? ? ': ' : "(#{params_str}): ") + "#{return_type}"
end

#resolve(registry) ⇒ Array<Store::Objects::Base>

Parameters:

  • registry (Registry)

Returns:



97
98
99
# File 'lib/yoda/model/type_expressions/function_type.rb', line 97

def resolve(registry)
  []
end

#to_rbs_type(env) ⇒ RBS::Types::Function

Parameters:

Returns:

  • (RBS::Types::Function)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/yoda/model/type_expressions/function_type.rb', line 113

def to_rbs_type(env)
  make_param = -> (param) { param&.to_rbs_param(env) }
  make_key_and_param = -> (param) { [param.name.to_sym, make_param.call(param)] }

  RBS::Types::Function.new(
    required_positionals: required_parameters.map(&make_param),
    optional_positionals: optional_parameters.map(&make_param),
    rest_positionals: make_param.call(rest_parameter),
    trailing_positionals: post_parameters.map(&make_param),
    # Not include keyword name to parameter object because if its string expression becomes redundunt.
    required_keywords: required_keyword_parameters.map(&make_key_and_param).to_h,
    optional_keywords: optional_keyword_parameters.map(&make_key_and_param).to_h,
    rest_keywords: make_param.call(keyword_rest_parameter),
    return_type: return_type&.to_rbs_type(env),
  )
end

#to_sObject



106
107
108
109
# File 'lib/yoda/model/type_expressions/function_type.rb', line 106

def to_s
  params_str = all_parameters_to_s
  (params_str.empty? ? '' : "(#{params_str}) -> ") + "#{return_type}"
end