File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /** Provides a module for constructing a union `Either` type. */
2+
3+ /** A type with `toString`. */
4+ signature class TypeWithToString {
5+ string toString ( ) ;
6+ }
7+
8+ /**
9+ * Constructs an `Either` type that is a disjoint union of two types.
10+ */
11+ module Either< TypeWithToString Left, TypeWithToString Right> {
12+ private newtype TEither =
13+ TLeft ( Left c ) or
14+ TRight ( Right c )
15+
16+ /**
17+ * An either type. This is either a `Left` or a `Right` wrapping the given
18+ * type.
19+ */
20+ class Either extends TEither {
21+ /** Gets a textual representation of this element. */
22+ string toString ( ) {
23+ exists ( Left c | this = TLeft ( c ) and result = c .toString ( ) )
24+ or
25+ exists ( Right c | this = TRight ( c ) and result = c .toString ( ) )
26+ }
27+
28+ /** Gets the element, if this is a `Left`. */
29+ Left asLeft ( ) { this = TLeft ( result ) }
30+
31+ /** Gets the element, if this is a `Right`. */
32+ Right asRight ( ) { this = TRight ( result ) }
33+ }
34+
35+ /** Makes an `Either` from an instanceof of `Left` */
36+ Left left ( Left c ) { result = c }
37+
38+ /** Makes an `Either` from an instanceof of `Right` */
39+ Right right ( Right c ) { result = c }
40+ }
You can’t perform that action at this time.
0 commit comments