Class: Hegel::Generators::DeferredGenerator

Inherits:
Hegel::Generator show all
Defined in:
lib/hegel/generators.rb,
sig/hegel.rbs

Overview

Hegel::Syntax::Methods#deferred. A forward reference to a generator whose definition is not known yet, so a generator can refer to itself (or to another deferred still being built) before #set installs the real one -- enabling self-recursive and mutually recursive generators, the same shape as hegel-rust's DeferredGeneratorDefinition, hegel-cpp's DeferredGeneratorDefinition, and hegel-java's Deferred:

tree = deferred
tree.set(one_of(integers, arrays(tree)))
tc.draw(tree)

Opens no span: #do_draw makes no native call of its own, only forwarding to whatever #set installed (one_of, above, already opens its own HEGEL_LABEL_ONE_OF span around that draw). This is the same "a generator opens a span only around more than one native call it makes itself" rule UuidsGenerator's own comment gives for a generator that makes exactly one -- applied here to a generator that makes zero. Every reference binding checked (hegel-rust's DeferredGenerator, hegel-cpp's DeferredGenerator, hegel-java's Deferred) opens none either.

Instance Method Summary collapse

Methods inherited from Hegel::Generator

#filter, #map

Constructor Details

#initializeDeferredGenerator

Returns a new instance of DeferredGenerator.



840
841
842
843
# File 'lib/hegel/generators.rb', line 840

def initialize
  super
  @inner = nil
end

Instance Method Details

#do_draw(tc) ⇒ Object

Parameters:

  • tc (Object)

Returns:

  • (Object)

Raises:



858
859
860
861
862
# File 'lib/hegel/generators.rb', line 858

def do_draw(tc)
  raise Hegel::Error, "deferred: draw called before set" unless @inner

  @inner.do_draw(tc)
end

#set(generator) ⇒ void

This method returns an undefined value.

Installs the generator this reference forwards to. May be called only once: hegel-java's Deferred#set raises IllegalStateException on a second call and hegel-cpp's throws; hegel-rust's consumes self, making a second call a compile error. Three independent bindings agree a second #set is a caller mistake, not a silent overwrite or no-op, so this raises the same way.

Parameters:

Raises:



852
853
854
855
856
# File 'lib/hegel/generators.rb', line 852

def set(generator)
  raise Hegel::Error, "deferred: set called more than once" if @inner

  @inner = generator
end