Class: Puppet::Pops::Types::POptionalType

Inherits:
PTypeWithContainedType show all
Defined in:
lib/puppet/pops/types/types.rb

Overview

Represents a type that accept PUndefType instead of the type parameter required_type - is a short hand for Variant[T, Undef]

Constant Summary collapse

DEFAULT =
POptionalType.new(nil)

Instance Attribute Summary

Attributes inherited from PTypeWithContainedType

#type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PTypeWithContainedType

#accept, #eql?, #generalize, #hash, #initialize, #resolve

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #eql?, #generalize, #hash, #iterable?, #iterable_type, #loader, #name, new_function, #really_instance?, #resolve, #roundtrip_with_string?, #simple_name, simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_pcore_type, create_ptype, register_ptypes

Methods included from Visitable

#accept

Methods included from PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s

Constructor Details

This class inherits a constructor from Puppet::Pops::Types::PTypeWithContainedType

Class Method Details

.register_ptype(loader, ir) ⇒ Object



3246
3247
3248
3249
3250
3251
3252
3253
# File 'lib/puppet/pops/types/types.rb', line 3246

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'AnyType',
    'type' => {
      KEY_TYPE => POptionalType.new(PTypeType::DEFAULT),
      KEY_VALUE => nil
    }
  )
end

Instance Method Details

#instance?(o, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


3263
3264
3265
# File 'lib/puppet/pops/types/types.rb', line 3263

def instance?(o, guard = nil)
  PUndefType::DEFAULT.instance?(o, guard) || (!@type.nil? && @type.instance?(o, guard))
end

#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


3259
3260
3261
# File 'lib/puppet/pops/types/types.rb', line 3259

def kind_of_callable?(optional=true, guard = nil)
    optional && !@type.nil? && @type.kind_of_callable?(optional, guard)
end

#new_functionObject



3284
3285
3286
# File 'lib/puppet/pops/types/types.rb', line 3284

def new_function
  optional_type.new_function
end

#normalize(guard = nil) ⇒ Object



3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
# File 'lib/puppet/pops/types/types.rb', line 3267

def normalize(guard = nil)
  n = super
  if n.type.nil?
    n
  else
    if n.type.is_a?(PNotUndefType)
      # No point in having an NotUndef in an Optional
      POptionalType.new(n.type.type).normalize
    elsif n.type.assignable?(PUndefType::DEFAULT)
      # THe type is Optional anyway, so it can be stripped of
      n.type
    else
      n
    end
  end
end

#optional_typeObject



3255
3256
3257
# File 'lib/puppet/pops/types/types.rb', line 3255

def optional_type
  @type
end