diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-01-05 21:05:07 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-01-05 21:05:07 +0000 |
| commit | 784de38fe0e07d827d6abf3f85ca34f441768a1c (patch) | |
| tree | 8ea0a1d07670196211d066cf9bc650a220cebca6 /src/main/java | |
| parent | 2420aca302718622531b9cbcbfaf4cd91c1889a1 (diff) | |
Add commonSubtype()
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/types/Types.java | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/main/java/org/shedlang/compiler/types/Types.java b/src/main/java/org/shedlang/compiler/types/Types.java index 9ab5c634..ab48f77e 100644 --- a/src/main/java/org/shedlang/compiler/types/Types.java +++ b/src/main/java/org/shedlang/compiler/types/Types.java @@ -89,4 +89,18 @@ public class Types { return TOP; } } + + public static Type commonSubtype(List<Type> supertypes) { + var uniqueSubtypes = supertypes.stream() + .filter(subtype -> subtype != Types.TOP) + .collect(Collectors.toSet()); + + if (uniqueSubtypes.isEmpty()) { + return TOP; + } else if (uniqueSubtypes.size() == 1) { + return uniqueSubtypes.iterator().next(); + } else { + return BOTTOM; + } + } } |
