diff --git a/lib/types.rb b/lib/types.rb index 2b2a86a371d16f78a33124cde9874dd40ca3428c..28a66415327fa69476600999ab628d929b031fe0 100644 --- a/lib/types.rb +++ b/lib/types.rb @@ -325,6 +325,26 @@ module Dhallish end end + class Union + attr_accessor :init_label + attr_accessor :init_val + attr_accessor :type + + def initialize(init_label, init_val, type) + @init_label = init_label + @init_val = init_val + @type = type + end + + def select(label) + if label == @init_label + @init_val + else + nil + end + end + end + # To be used as Dhallish::Value.val for ruby-defined functions class BuiltinFunction attr_accessor :block diff --git a/tests/test.rb b/tests/test.rb index 9dd57a0a4a761401cbda8a81c60eaac0064cc461..28df652c23f4cb8c223dc79464c4dea4929194ec 100755 --- a/tests/test.rb +++ b/tests/test.rb @@ -34,7 +34,8 @@ positive_tests = { "identity with type parameter": ["let f = \\(t : Type) -> \\(x : t) -> x in (f : forall(t : Type) -> t -> t) Natural 2", 2], "function with two type parameters": ["let f = \\(t : Type) -> \\(u : Type) -> \\(x : t) -> \\(y : u) -> x in (f : forall(t : Type) -> forall(u : Type) -> t -> u -> t) Natural Integer 1 -1 ", 1 ], "fallback for file import": ["let x = ./foobarfoobar.dhall as Text ? 1 in x", 1], - "more type annotation": [ "let f = \\(a: Type) -> \\(l: List a) -> \\(x: a) -> x in (f: forall(b: Type) -> forall(l: List b) -> b -> b) Natural [1, 2, 3] 5", 5 ] + "more type annotation": [ "let f = \\(a: Type) -> \\(l: List a) -> \\(x: a) -> x in (f: forall(b: Type) -> forall(l: List b) -> b -> b) Natural [1, 2, 3] 5", 5 ], + "select from union": [ "let union = <a : Natural | b : Text>.a 17 in union.a : Optional Natural", 17 ] } negative_tests = {