Class: Dhall::UnionType

Inherits:
Expression show all
Defined in:
lib/dhall/ast.rb,
lib/dhall/binary.rb,
lib/dhall/normalize.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expression

#&, #*, #+, #annotate, #as_dhall, #cache_key, #call, #concat, #deep_merge, #deep_merge_type, #dhall_eq, #digest, #fusion, #resolve, #shift, #slice, #substitute, #to_binary, #to_cbor, #to_proc, #to_s, #|

Class Method Details

.decode(record) ⇒ Object



176
177
178
179
180
# File 'lib/dhall/binary.rb', line 176

def self.decode(record)
	new(alternatives: Hash[record.map do |k, v|
		[k, v.nil? ? v : Dhall.decode(v)]
	end])
end

Instance Method Details

#==(other) ⇒ Object



974
975
976
# File 'lib/dhall/ast.rb', line 974

def ==(other)
	other.is_a?(UnionType) && alternatives.to_a == other.alternatives.to_a
end

#[](k) ⇒ Object



961
962
963
# File 'lib/dhall/ast.rb', line 961

def [](k)
	alternatives.fetch(k)
end

#as_jsonObject



1012
1013
1014
# File 'lib/dhall/ast.rb', line 1012

def as_json
	[11, Hash[alternatives.to_a.map { |k, v| [k, v&.as_json] }.sort]]
end

#constructor_typesObject



1002
1003
1004
1005
1006
1007
1008
1009
1010
# File 'lib/dhall/ast.rb', line 1002

def constructor_types
	alternatives.each_with_object({}) do |(k, type), ctypes|
		ctypes[k] = if type.nil?
			self
		else
			Forall.new(var: k, type: type, body: self)
		end
	end
end

#empty?Boolean

Returns:

  • (Boolean)


957
958
959
# File 'lib/dhall/ast.rb', line 957

def empty?
	alternatives.empty?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


978
979
980
# File 'lib/dhall/ast.rb', line 978

def eql?(other)
	self == other
end

#fetch(k, default = nil) ⇒ Object



986
987
988
989
990
991
992
993
994
# File 'lib/dhall/ast.rb', line 986

def fetch(k, default=nil)
	if alternatives.fetch(k)
		super(k)
	else
		Union.from(self, k, nil)
	end
rescue KeyError
	block_given? ? yield : (default || raise)
end

#get_constructor(selector) ⇒ Object



996
997
998
999
1000
# File 'lib/dhall/ast.rb', line 996

def get_constructor(selector)
	type = alternatives.fetch(selector)
	body = Union.from(self, selector, Variable[selector])
	Function.new(var: selector, type: type, body: body)
end

#merge(other, &block) ⇒ Object



982
983
984
# File 'lib/dhall/ast.rb', line 982

def merge(other, &block)
	with(alternatives: alternatives.merge(other.alternatives, &block))
end

#normalizeObject



372
373
374
# File 'lib/dhall/normalize.rb', line 372

def normalize
	with(alternatives: Hash[super.alternatives.sort])
end

#recordObject



970
971
972
# File 'lib/dhall/ast.rb', line 970

def record
	alternatives
end

#without(*keys) ⇒ Object



965
966
967
968
# File 'lib/dhall/ast.rb', line 965

def without(*keys)
	keys.map!(&:to_s)
	with(alternatives: alternatives.reject { |k, _| keys.include?(k) })
end