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, #eql?, #generalize, #hash, #iterable?, #iterable_type, #name, new_function, #really_instance?, #resolve, #simple_name, simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_ptype, create_ptype, register_ptypes

Methods included from Visitable

#accept

Methods included from PuppetObject

#_ptype

Constructor Details

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

Class Method Details

.register_ptype(loader, ir) ⇒ Object



3029
3030
3031
3032
3033
3034
3035
3036
# File 'lib/puppet/pops/types/types.rb', line 3029

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

Instance Method Details

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



3046
3047
3048
# File 'lib/puppet/pops/types/types.rb', line 3046

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



3042
3043
3044
# File 'lib/puppet/pops/types/types.rb', line 3042

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

#new_function(loader) ⇒ Object



3067
3068
3069
# File 'lib/puppet/pops/types/types.rb', line 3067

def new_function(loader)
  optional_type.new_function(loader)
end

#normalize(guard = nil) ⇒ Object



3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
# File 'lib/puppet/pops/types/types.rb', line 3050

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



3038
3039
3040
# File 'lib/puppet/pops/types/types.rb', line 3038

def optional_type
  @type
end