Class: Steep::Interface::MethodType

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/interface/method_type.rb

Constant Summary collapse

NONE =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_params:, params:, block:, return_type:, location:) ⇒ MethodType

Returns a new instance of MethodType.



279
280
281
282
283
284
285
# File 'lib/steep/interface/method_type.rb', line 279

def initialize(type_params:, params:, block:, return_type:, location:)
  @type_params = type_params
  @params = params
  @block = block
  @return_type = return_type
  @location = location
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



273
274
275
# File 'lib/steep/interface/method_type.rb', line 273

def block
  @block
end

#locationObject (readonly)

Returns the value of attribute location.



275
276
277
# File 'lib/steep/interface/method_type.rb', line 275

def location
  @location
end

#paramsObject (readonly)

Returns the value of attribute params.



272
273
274
# File 'lib/steep/interface/method_type.rb', line 272

def params
  @params
end

#return_typeObject (readonly)

Returns the value of attribute return_type.



274
275
276
# File 'lib/steep/interface/method_type.rb', line 274

def return_type
  @return_type
end

#type_paramsObject (readonly)

Returns the value of attribute type_params.



271
272
273
# File 'lib/steep/interface/method_type.rb', line 271

def type_params
  @type_params
end

Instance Method Details

#==(other) ⇒ Object



287
288
289
290
291
292
293
294
# File 'lib/steep/interface/method_type.rb', line 287

def ==(other)
  other.is_a?(self.class) &&
    other.type_params == type_params &&
    other.params == params &&
    other.block == block &&
    other.return_type == return_type &&
    (!other.location || !location || other.location == location)
end

#each_type(&block) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/steep/interface/method_type.rb', line 312

def each_type(&block)
  if block_given?
    params.each_type(&block)
    self.block&.tap do
      self.block.type.params.each_type(&block)
      yield(self.block.type.return_type)
    end
    yield(return_type)
  else
    enum_for :each_type
  end
end

#free_variablesObject



296
297
298
# File 'lib/steep/interface/method_type.rb', line 296

def free_variables
  (params.free_variables + (block&.free_variables || Set.new) + return_type.free_variables) - Set.new(type_params)
end

#instantiate(s) ⇒ Object



325
326
327
328
329
330
331
332
333
# File 'lib/steep/interface/method_type.rb', line 325

def instantiate(s)
  self.class.new(
    type_params: [],
    params: params.subst(s),
    block: block&.subst(s),
    return_type: return_type.subst(s),
    location: location,
    )
end

#subst(s) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
# File 'lib/steep/interface/method_type.rb', line 300

def subst(s)
  s_ = s.except(type_params)

  self.class.new(
    type_params: type_params,
    params: params.subst(s_),
    block: block&.subst(s_),
    return_type: return_type.subst(s_),
    location: location
  )
end

#to_sObject



345
346
347
348
349
350
351
# File 'lib/steep/interface/method_type.rb', line 345

def to_s
  type_params = !self.type_params.empty? ? "<#{self.type_params.map{|x| "'#{x}" }.join(", ")}> " : ""
  params = self.params.to_s
  block = self.block ? " #{self.block}" : ""

  "#{type_params}#{params}#{block} -> #{return_type}"
end

#with(type_params: NONE, params: NONE, block: NONE, return_type: NONE, location: NONE) ⇒ Object



335
336
337
338
339
340
341
342
343
# File 'lib/steep/interface/method_type.rb', line 335

def with(type_params: NONE, params: NONE, block: NONE, return_type: NONE, location: NONE)
  self.class.new(
    type_params: type_params.equal?(NONE) ? self.type_params : type_params,
    params: params.equal?(NONE) ? self.params : params,
    block: block.equal?(NONE) ? self.block : block,
    return_type: return_type.equal?(NONE) ? self.return_type : return_type,
    location: location.equal?(NONE) ? self.location : location
  )
end