class Dhallish::Ast::RecordUnionSelector

rec: ast node of something that will be a record, key: name of key to access in record (string)

Attributes

key[RW]
rec[RW]

Public Class Methods

new(rec, key) click to toggle source
# File lib/ast.rb, line 429
def initialize(rec, key)
        @rec = rec
        @key = key
end

Public Instance Methods

compute_type(ctx) click to toggle source
# File lib/ast.rb, line 434
def compute_type(ctx)
        rectype = @rec.compute_type ctx
        assert("`.` can only be used on records and unions, the key `#{@key}` must exist") {
                ((rectype.is_a? Types::Record or rectype.is_a? Types::Union) and !rectype.types[@key].nil?) \
                or (rectype.is_a? Types::Type and rectype.metadata.is_a? Types::Union)
        }
        if rectype.is_a? Types::Union
                Types::Optional.new rectype.types[@key]
        elsif rectype.is_a? Types::Record
                rectype.types[@key]
        else
                union_type = rectype.metadata
                Types::Function.new union_type.types[@key], union_type
        end
end
evaluate(ctx) click to toggle source
# File lib/ast.rb, line 450
def evaluate(ctx)
        rec = @rec.evaluate ctx
        if rec.is_a? Union
                rec.select @key
        elsif rec.is_a? Hash  # <== Record
                rec[@key]
        else
                BuiltinFunction.new { |val| Union.new @key, val, rec }
        end
end