Skip to content

Commit 50a91b5

Browse files
committed
shared: add a shared Either type
1 parent 073e9bc commit 50a91b5

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

shared/util/codeql/util/Either.qll

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

0 commit comments

Comments
 (0)