Class: Steep::Interface::Function::Params::PositionalParams

Inherits:
Object
  • Object
show all
Extended by:
Utils
Defined in:
lib/steep/interface/function.rb

Defined Under Namespace

Classes: Base, Optional, Required, Rest

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

intersection, union

Constructor Details

#initialize(head:, tail:) ⇒ PositionalParams

Returns a new instance of PositionalParams.



64
65
66
67
# File 'lib/steep/interface/function.rb', line 64

def initialize(head:, tail:)
  @head = head
  @tail = tail
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



61
62
63
# File 'lib/steep/interface/function.rb', line 61

def head
  @head
end

#tailObject (readonly)

Returns the value of attribute tail.



62
63
64
# File 'lib/steep/interface/function.rb', line 62

def tail
  @tail
end

Class Method Details

.build(required:, optional:, rest:) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/steep/interface/function.rb', line 148

def self.build(required:, optional:, rest:)
  params = rest ? self.rest(rest) : nil
  params = optional.reverse_each.inject(params) {|params, type| self.optional(type, params) }
  params = required.reverse_each.inject(params) {|params, type| self.required(type, params) }

  params
end

.merge_for_intersection(xs, ys) ⇒ Object

Calculates xs & ys. Raises when failed.



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/steep/interface/function.rb', line 356

def self.merge_for_intersection(xs, ys)
  x = xs&.head
  y = ys&.head

  case
  when x.is_a?(Required) && y.is_a?(Required)
    xs or raise
    ys or raise
    required(
      intersection(x.type, y.type),
      merge_for_intersection(xs.tail, ys.tail)
    )
  when x.is_a?(Required) && !y
    raise
  when x.is_a?(Required) && y.is_a?(Optional)
    xs or raise
    ys or raise
    required(
      intersection(x.type, y.type),
      merge_for_intersection(xs.tail, ys.tail)
    )
  when x.is_a?(Required) && y.is_a?(Rest)
    xs or raise
    ys or raise
    required(
      intersection(x.type, y.type),
      merge_for_intersection(xs.tail, ys)
    )
  when !x && y.is_a?(Required)
    raise
  when !x && !y
    nil
  when !x && y.is_a?(Optional)
    nil
  when !x && y.is_a?(Rest)
    nil
  when x.is_a?(Optional) && y.is_a?(Required)
    xs or raise
    ys or raise
    required(
      intersection(x.type, y.type),
      merge_for_intersection(xs.tail, ys.tail)
    )
  when x.is_a?(Optional) && !y
    nil
  when x.is_a?(Optional) && y.is_a?(Optional)
    xs or raise
    ys or raise
    optional(
      intersection(x.type, y.type),
      merge_for_intersection(xs.tail, ys.tail)
    )
  when x.is_a?(Optional) && y.is_a?(Rest)
    xs or raise
    ys or raise
    optional(
      intersection(x.type, y.type),
      merge_for_intersection(xs.tail, ys)
    )
  when x.is_a?(Rest) && y.is_a?(Required)
    xs or raise
    ys or raise
    required(
      intersection(x.type, y.type),
      merge_for_intersection(xs, ys.tail)
    )
  when x.is_a?(Rest) && !y
    nil
  when x.is_a?(Rest) && y.is_a?(Optional)
    xs or raise
    ys or raise
    optional(
      intersection(x.type, y.type),
      merge_for_intersection(xs, ys.tail)
    )
  when x.is_a?(Rest) && y.is_a?(Rest)
    rest(intersection(x.type, y.type))
  end
end

.merge_for_overload(xs, ys) ⇒ Object

Calculates xs + ys. Never fails.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/steep/interface/function.rb', line 160

def self.merge_for_overload(xs, ys)
  x = xs&.head
  y = ys&.head

  case
  when x.is_a?(Required) && y.is_a?(Required)
    xs or raise
    ys or raise
    required(
      union(x.type, y.type),
      merge_for_overload(xs.tail, ys.tail)
    )
  when x.is_a?(Required) && y.is_a?(Optional)
    xs or raise
    ys or raise
    optional(
      union(x.type, y.type, null: true),
      merge_for_overload(xs.tail, ys.tail)
    )
  when x.is_a?(Required) && y.is_a?(Rest)
    xs or raise
    ys or raise
    optional(
      union(x.type, y.type, null: true),
      merge_for_overload(xs.tail, ys)
    )
  when x.is_a?(Required) && !y
    xs or raise
    optional(
      union(x.type, null: true),
      merge_for_overload(xs.tail, nil)
    )
  when x.is_a?(Optional) && y.is_a?(Required)
    xs or raise
    ys or raise
    optional(
      union(x.type, y.type, null: true),
      merge_for_overload(xs.tail, ys.tail)
    )
  when x.is_a?(Optional) && y.is_a?(Optional)
    xs or raise
    ys or raise
    optional(
      union(x.type, y.type),
      merge_for_overload(xs.tail, ys.tail)
    )
  when x.is_a?(Optional) && y.is_a?(Rest)
    xs or raise
    ys or raise
    optional(
      union(x.type, y.type),
      merge_for_overload(xs.tail, ys)
    )
  when x.is_a?(Optional) && !y
    xs or raise
    optional(
      x.type,
      merge_for_overload(xs.tail, nil)
    )  # == xs
  when x.is_a?(Rest) && y.is_a?(Required)
    xs or raise
    ys or raise
    optional(
      union(x.type, y.type, null: true),
      merge_for_overload(xs, ys.tail)
    )
  when x.is_a?(Rest) && y.is_a?(Optional)
    xs or raise
    ys or raise
    optional(
      union(x.type, y.type),
      merge_for_overload(xs, ys.tail)
    )
  when x.is_a?(Rest) && y.is_a?(Rest)
    xs or raise
    ys or raise
    rest(union(x.type, y.type))
  when x.is_a?(Rest) && !y
    xs or raise
  when !x && y.is_a?(Required)
    ys or raise
    optional(
      union(y.type, null: true),
      merge_for_overload(nil, ys.tail)
    )
  when !x && y.is_a?(Optional)
    ys or raise
    optional(
      y.type,
      merge_for_overload(nil, ys.tail)
    )  # == ys
  when !x && y.is_a?(Rest)
    ys or raise
  when !x && !y
    nil
  end
end

.merge_for_union(xs, ys) ⇒ Object

xs | ys



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/steep/interface/function.rb', line 259

def self.merge_for_union(xs, ys)
  x = xs&.head
  y = ys&.head

  case
  when x.is_a?(Required) && y.is_a?(Required)
    xs or raise
    ys or raise
    required(
      union(x.type, y.type),
      merge_for_union(xs.tail, ys.tail)
    )
  when x.is_a?(Required) && !y
    xs or raise
    optional(
      x.type,
      merge_for_union(xs.tail, nil)
    )
  when x.is_a?(Required) && y.is_a?(Optional)
    xs or raise
    ys or raise
    optional(
      union(x.type, y.type),
      merge_for_union(xs.tail, ys.tail)
    )
  when x.is_a?(Required) && y.is_a?(Rest)
    xs or raise
    ys or raise
    optional(
      union(x.type, y.type),
      merge_for_union(xs.tail, ys)
    )
  when !x && y.is_a?(Required)
    ys or raise
    optional(
      y.type,
      merge_for_union(nil, ys.tail)
    )
  when !x && !y
    nil
  when !x && y.is_a?(Optional)
    ys or raise
    PositionalParams.new(head: y, tail: merge_for_union(nil, ys.tail))
  when !x && y.is_a?(Rest)
    ys or raise
  when x.is_a?(Optional) && y.is_a?(Required)
    xs or raise
    ys or raise
    optional(
      union(x.type, y.type),
      merge_for_union(xs.tail, ys.tail)
    )
  when x.is_a?(Optional) && !y
    xs or raise
    PositionalParams.new(head: x, tail: merge_for_union(xs.tail, nil)) # == xs
  when x.is_a?(Optional) && y.is_a?(Optional)
    xs or raise
    ys or raise
    optional(
      union(x.type, y.type),
      merge_for_union(xs.tail, ys.tail)
    )
  when x.is_a?(Optional) && y.is_a?(Rest)
    xs or raise
    ys or raise
    optional(
      union(x.type, y.type),
      merge_for_union(xs.tail, ys.tail)
    )
  when x.is_a?(Rest) && y.is_a?(Required)
    xs or raise
    ys or raise
    optional(
      union(x.type, y.type),
      merge_for_union(xs, ys.tail)
    )
  when x.is_a?(Rest) && !y
    xs or raise
  when x.is_a?(Rest) && y.is_a?(Optional)
    xs or raise
    ys or raise
    optional(
      union(x.type, y.type),
      merge_for_union(xs, ys.tail)
    )
  when x.is_a?(Rest) && y.is_a?(Rest)
    xs or raise
    ys or raise
    rest(
      union(x.type, y.type)
    )
  end
end

.optional(type, tail = nil) ⇒ Object



73
74
75
# File 'lib/steep/interface/function.rb', line 73

def self.optional(type, tail = nil)
  PositionalParams.new(head: Optional.new(type), tail: tail)
end

.required(type, tail = nil) ⇒ Object



69
70
71
# File 'lib/steep/interface/function.rb', line 69

def self.required(type, tail = nil)
  PositionalParams.new(head: Required.new(type), tail: tail)
end

.rest(type, tail = nil) ⇒ Object



77
78
79
# File 'lib/steep/interface/function.rb', line 77

def self.rest(type, tail = nil)
  PositionalParams.new(head: Rest.new(type), tail: tail)
end

Instance Method Details

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



115
116
117
# File 'lib/steep/interface/function.rb', line 115

def ==(other)
  other.is_a?(PositionalParams) && other.head == head && other.tail == tail
end

#each(&block) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/steep/interface/function.rb', line 125

def each(&block)
  if block
    yield head
    tail&.each(&block)
  else
    enum_for(:each)
  end
end

#each_typeObject



134
135
136
137
138
139
140
141
142
# File 'lib/steep/interface/function.rb', line 134

def each_type
  if block_given?
    each do |param|
      yield param.type
    end
  else
    enum_for :each_type
  end
end

#hashObject



121
122
123
# File 'lib/steep/interface/function.rb', line 121

def hash
  self.class.hash ^ head.hash ^ tail.hash
end

#map(&block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/steep/interface/function.rb', line 85

def map(&block)
  hd = yield(head)
  tl = tail&.map(&block)

  if head == hd && tail == tl
    self
  else
    PositionalParams.new(head: hd, tail: tl)
  end
end

#map_type(&block) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/steep/interface/function.rb', line 96

def map_type(&block)
  if block
    map {|param| param.map_type(&block) }
  else
    enum_for :map_type
  end
end

#sizeObject



144
145
146
# File 'lib/steep/interface/function.rb', line 144

def size
  1 + (tail&.size || 0)
end

#subst(s) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/steep/interface/function.rb', line 104

def subst(s)
  map_type do |type|
    ty = type.subst(s)
    if ty == type
      type
    else
      ty
    end
  end
end

#to_aryObject



81
82
83
# File 'lib/steep/interface/function.rb', line 81

def to_ary
  [head, tail]
end