Class: Typisch::Type::NamedPlaceholder

Inherits:
Typisch::Type show all
Defined in:
lib/typisch/named_placeholder.rb

Overview

This is a proxy wrapper for a type, which we can use as a placeholder for a named type which hasn’t yet been declared. Helps when it comes to cyclic references etc.

(You can view this as a free variable, where the scope of all free variables is

implicitly closed over at the top level by the 'registry'. We don't keep variables
lying around as symbolic things in a syntax tree though, we're just using them as
temporary placeholders on the way to rewriting it as a syntax *graph*).

Once a register_types block has finished, the registry ensures that all references in the type graph to NamedPlaceholders are replaced with references to their targets.

Instance Attribute Summary collapse

Attributes inherited from Typisch::Type

#name

Instance Method Summary collapse

Methods inherited from Typisch::Type

#<, #<=, #<=>, #==, #===, #>, #>=, #alternative_types, #annotations, #annotations=, #canonicalize!, #excluding_null, #inspect, #recursive?, #shallow_check_type, #subexpression_types, subtype?, #to_string

Constructor Details

#initialize(name, registry) ⇒ NamedPlaceholder

Returns a new instance of NamedPlaceholder.



13
14
15
16
# File 'lib/typisch/named_placeholder.rb', line 13

def initialize(name, registry)
  @registry = registry
  @name = name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



59
60
61
# File 'lib/typisch/named_placeholder.rb', line 59

def method_missing(name, *args, &block)
  target.respond_to?(name) ? target.send(name, *args, &block) : super
end

Instance Attribute Details

#targetObject



18
19
20
21
22
23
24
# File 'lib/typisch/named_placeholder.rb', line 18

def target
  return @target if @target
  @target = @registry[@name]
  case @target when NilClass, Type::NamedPlaceholder
    raise NameResolutionError.new(@name.inspect)
  end
end

Instance Method Details

#classObject

this is slightly naughty - we actually pretend to be of the class of our target object.

note that TargetClass === placeholder will still return false.



38
39
40
# File 'lib/typisch/named_placeholder.rb', line 38

def class
  target.class
end

#instance_of?(klass) ⇒ Boolean

Returns:



47
48
49
# File 'lib/typisch/named_placeholder.rb', line 47

def instance_of?(klass)
  target.instance_of?(klass)
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

Returns:



42
43
44
# File 'lib/typisch/named_placeholder.rb', line 42

def is_a?(klass)
  target.is_a?(klass)
end

#respond_to?(name, include_private = false) ⇒ Boolean

Returns:



63
64
65
# File 'lib/typisch/named_placeholder.rb', line 63

def respond_to?(name, include_private=false)
  super || target.respond_to?(name, include_private)
end

#to_sObject



51
52
53
# File 'lib/typisch/named_placeholder.rb', line 51

def to_s(*)
  @name.inspect
end