class Dhallish::Ast::UnionMerge
Attributes
handler[RW]
union[RW]
Public Class Methods
new(handler, union)
click to toggle source
# File lib/ast.rb, line 740 def initialize(handler, union) @handler = handler @union = union end
Public Instance Methods
compute_type(ctx)
click to toggle source
# File lib/ast.rb, line 745 def compute_type(ctx) hdlr = @handler.compute_type ctx unin = @union.compute_type ctx assert("`merge` expects a record as first and a union as second argument (both with the same keys and they shall not be empty)") { hdlr.is_a? Types::Record and unin.is_a? Types::Union and hdlr.types.size == unin.types.size and hdlr.types.size > 0 } rettype = nil hdlr.types.each { |key, fntype| argtype = unin.types[key] assert("`merge` record (of functions) and union must have the same fields") { !argtype.nil? and fntype.is_a? Types::Function } if rettype.nil? rettype = fntype.restype else # TODO: unification instead of == assert("`merge` functions must all have the same return type") { fntype.restype == rettype } end assert("`merge` functions must take as argument the type of its union field") { fntype.argtype == argtype } } rettype end
evaluate(ctx)
click to toggle source
# File lib/ast.rb, line 767 def evaluate(ctx) hdlr = @handler.evaluate ctx unin = @union.evaluate ctx fn = hdlr[unin.init_label] fn.call(unin.init_val) end