Class: RBI::Type::Nilable

Inherits:
RBI::Type show all
Defined in:
lib/rbi/type.rb

Overview

A type that can be ‘nil` like `T.nilable(String)`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RBI::Type

all, any, anything, attached_class, boolean, class_of, #eql?, generic, #hash, #nilable, nilable, #nilable?, #non_nilable, noreturn, parse_node, parse_string, proc, #rbs_string, self_type, shape, simple, t_class, t_module, #to_s, tuple, type_alias, type_parameter, untyped, void

Constructor Details

#initialize(type) ⇒ Nilable

: (Type type) -> void



363
364
365
366
# File 'lib/rbi/type.rb', line 363

def initialize(type)
  super()
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

: Type



360
361
362
# File 'lib/rbi/type.rb', line 360

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object

: (BasicObject other) -> bool



370
371
372
# File 'lib/rbi/type.rb', line 370

def ==(other)
  Nilable === other && @type == other.type
end

#normalizeObject

: -> Type



382
383
384
# File 'lib/rbi/type.rb', line 382

def normalize
  Type::Any.new([Type.simple("NilClass"), @type.normalize])
end

#simplifyObject

: -> Type



388
389
390
391
392
393
394
395
396
397
# File 'lib/rbi/type.rb', line 388

def simplify
  case @type
  when Nilable
    @type.simplify
  when Untyped
    @type.simplify
  else
    self
  end
end

#to_rbiObject

: -> String



376
377
378
# File 'lib/rbi/type.rb', line 376

def to_rbi
  "::T.nilable(#{@type.to_rbi})"
end