Class: RBS::Types::Proc

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/types.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location:, type:, block:, self_type: nil) ⇒ Proc

Returns a new instance of Proc.



1245
1246
1247
1248
1249
1250
# File 'lib/rbs/types.rb', line 1245

def initialize(location:, type:, block:, self_type: nil)
  @type = type
  @block = block
  @location = location
  @self_type = self_type
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



1241
1242
1243
# File 'lib/rbs/types.rb', line 1241

def block
  @block
end

#locationObject (readonly)

Returns the value of attribute location.



1243
1244
1245
# File 'lib/rbs/types.rb', line 1243

def location
  @location
end

#self_typeObject (readonly)

Returns the value of attribute self_type.



1242
1243
1244
# File 'lib/rbs/types.rb', line 1242

def self_type
  @self_type
end

#typeObject (readonly)

Returns the value of attribute type.



1240
1241
1242
# File 'lib/rbs/types.rb', line 1240

def type
  @type
end

Instance Method Details

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



1252
1253
1254
# File 'lib/rbs/types.rb', line 1252

def ==(other)
  other.is_a?(Proc) && other.type == type && other.block == block && other.self_type == self_type
end

#each_type(&block) ⇒ Object



1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
# File 'lib/rbs/types.rb', line 1304

def each_type(&block)
  if block
    type.each_type(&block)
    self.block&.type&.each_type(&block)
    if self_type = self.block&.self_type
      yield self_type
    end
  else
    enum_for :each_type
  end
end

#free_variables(set = ) ⇒ Object



1262
1263
1264
1265
1266
1267
# File 'lib/rbs/types.rb', line 1262

def free_variables(set = Set[])
  type.free_variables(set)
  block&.type&.free_variables(set)
  self_type&.free_variables(set)
  set
end

#has_classish_type?Boolean

Returns:

  • (Boolean)


1342
1343
1344
# File 'lib/rbs/types.rb', line 1342

def has_classish_type?
  each_type.any? {|type| type.has_classish_type? }
end

#has_self_type?Boolean

Returns:

  • (Boolean)


1338
1339
1340
# File 'lib/rbs/types.rb', line 1338

def has_self_type?
  each_type.any? {|type| type.has_self_type? }
end

#hashObject



1258
1259
1260
# File 'lib/rbs/types.rb', line 1258

def hash
  self.class.hash ^ type.hash ^ block.hash ^ self_type.hash
end

#map_type(&block) ⇒ Object



1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
# File 'lib/rbs/types.rb', line 1325

def map_type(&block)
  if block
    Proc.new(
      type: type.map_type(&block),
      block: self.block&.map_type(&block),
      self_type: self_type ? yield(self_type) : nil,
      location: location
    )
  else
    enum_for :map_type
  end
end

#map_type_name(&block) ⇒ Object



1316
1317
1318
1319
1320
1321
1322
1323
# File 'lib/rbs/types.rb', line 1316

def map_type_name(&block)
  Proc.new(
    type: type.map_type_name(&block),
    block: self.block&.map_type {|type| type.map_type_name(&block) },
    self_type: self_type&.map_type_name(&block),
    location: location
  )
end

#sub(s) ⇒ Object



1279
1280
1281
1282
1283
1284
1285
1286
# File 'lib/rbs/types.rb', line 1279

def sub(s)
  self.class.new(
    type: type.sub(s),
    block: block&.sub(s),
    self_type: self_type&.sub(s),
    location: location
  )
end

#to_json(state = _ = nil) ⇒ Object



1269
1270
1271
1272
1273
1274
1275
1276
1277
# File 'lib/rbs/types.rb', line 1269

def to_json(state = _ = nil)
  {
    class: :proc,
    type: type,
    block: block,
    location: location,
    self_type: self_type
  }.to_json(state)
end

#to_s(level = 0) ⇒ Object



1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
# File 'lib/rbs/types.rb', line 1288

def to_s(level = 0)
  self_binding = SelfTypeBindingHelper.self_type_binding_to_s(self_type)
  block_self_binding = SelfTypeBindingHelper.self_type_binding_to_s(block&.self_type)

  case
  when b = block
    if b.required
      "^(#{type.param_to_s}) #{self_binding}{ (#{b.type.param_to_s}) #{block_self_binding}-> #{b.type.return_to_s} } -> #{type.return_to_s}"
    else
      "^(#{type.param_to_s}) #{self_binding}?{ (#{b.type.param_to_s}) #{block_self_binding}-> #{b.type.return_to_s} } -> #{type.return_to_s}"
    end
  else
    "^(#{type.param_to_s}) #{self_binding}-> #{type.return_to_s}"
  end
end

#with_nonreturn_void?Boolean

Returns:

  • (Boolean)


1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
# File 'lib/rbs/types.rb', line 1346

def with_nonreturn_void?
  if type.with_nonreturn_void?
    true
  else
    if block = block()
      block.type.with_nonreturn_void? || block.self_type&.with_nonreturn_void? || false
    else
      false
    end
  end
end