Module: Jade::Codegen::Boundary::Specialized::Maybe

Extended by:
Maybe
Included in:
Maybe
Defined in:
lib/jade/codegen/boundary/specialized/maybe.rb

Overview

Maybe(t) where t is itself specializable. Both decode and encode bind the input via .then { it ... } so a complex value_expr (e.g. a full Internal.X(...) call) isn't re-evaluated.

Instance Method Summary collapse

Instance Method Details

#decode(type, input, registry) ⇒ Object



12
13
14
15
16
17
# File 'lib/jade/codegen/boundary/specialized/maybe.rb', line 12

def decode(type, input, registry)
  inner = inner_of(type) or return nil
  elem = Specialized.decode_expr(inner, 'it', registry) or return nil

  "#{input}.then { it.nil? ? Jade::Maybe::Nothing[] : Jade::Maybe::Just[#{elem}] }"
end

#element_encoder(inner, registry) ⇒ Object

Never identity even when the element is: the Just still unwraps.



30
31
32
33
34
35
36
# File 'lib/jade/codegen/boundary/specialized/maybe.rb', line 30

def element_encoder(inner, registry)
  case Specialized.encode_expr(inner, 'it._1', registry)
  in :identity then 'it._1'
  in ::String => expr then expr
  in nil then nil
  end
end

#encode(type, value_expr, registry) ⇒ Object



19
20
21
22
23
# File 'lib/jade/codegen/boundary/specialized/maybe.rb', line 19

def encode(type, value_expr, registry)
  inner_of(type)
    &.then { element_encoder(it, registry) }
    &.then { unwrap_expr(it, value_expr) }
end

#inner_of(type) ⇒ Object



43
44
45
46
47
# File 'lib/jade/codegen/boundary/specialized/maybe.rb', line 43

def inner_of(type)
  return nil unless Specialized.qname_of(type) == 'Maybe.Maybe'
  args = Specialized.args_of(type)
  args&.size == 1 ? args[0] : nil
end

#specializable?(type, registry, seen) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/jade/codegen/boundary/specialized/maybe.rb', line 38

def specializable?(type, registry, seen)
  inner = inner_of(type) or return false
  Specialized.specializable_field?(inner, registry, seen)
end

#unwrap_expr(elem, value_expr) ⇒ Object



25
26
27
# File 'lib/jade/codegen/boundary/specialized/maybe.rb', line 25

def unwrap_expr(elem, value_expr)
  "#{value_expr}.then { it.is_a?(::Jade::Maybe::Just) ? #{elem} : nil }"
end