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.



250
251
252
253
254
255
256
# File 'lib/steep/interface/method_type.rb', line 250

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.



244
245
246
# File 'lib/steep/interface/method_type.rb', line 244

def block
  @block
end

#locationObject (readonly)

Returns the value of attribute location.



246
247
248
# File 'lib/steep/interface/method_type.rb', line 246

def location
  @location
end

#paramsObject (readonly)

Returns the value of attribute params.



243
244
245
# File 'lib/steep/interface/method_type.rb', line 243

def params
  @params
end

#return_typeObject (readonly)

Returns the value of attribute return_type.



245
246
247
# File 'lib/steep/interface/method_type.rb', line 245

def return_type
  @return_type
end

#type_paramsObject (readonly)

Returns the value of attribute type_params.



242
243
244
# File 'lib/steep/interface/method_type.rb', line 242

def type_params
  @type_params
end

Instance Method Details

#==(other) ⇒ Object



258
259
260
261
262
263
264
265
# File 'lib/steep/interface/method_type.rb', line 258

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



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

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



267
268
269
# File 'lib/steep/interface/method_type.rb', line 267

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

#instantiate(s) ⇒ Object



296
297
298
299
300
301
302
303
304
# File 'lib/steep/interface/method_type.rb', line 296

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



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/steep/interface/method_type.rb', line 271

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



316
317
318
319
320
321
322
# File 'lib/steep/interface/method_type.rb', line 316

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



306
307
308
309
310
311
312
313
314
# File 'lib/steep/interface/method_type.rb', line 306

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