]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
(refs #7433) Make Either type available in Java 52/852/2
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 16 Aug 2017 06:06:31 +0000 (09:06 +0300)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 16 Aug 2017 06:21:00 +0000 (09:21 +0300)
Change-Id: I9ee7e4ca0bb2bf682b00447d8a44568276e74083

bundles/org.simantics.scl.runtime/META-INF/MANIFEST.MF
bundles/org.simantics.scl.runtime/scl/Prelude.scl
bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Either.java [new file with mode: 0644]
bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Left.java [new file with mode: 0644]
bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Right.java [new file with mode: 0644]

index aec2f1b850535e8e127a25d876d605a251d6d0be..382af3fc4e74750e2e7063d2e59e0eda9646acdd 100644 (file)
@@ -7,6 +7,7 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Export-Package: org.simantics.scl.runtime,
  org.simantics.scl.runtime.chr,
  org.simantics.scl.runtime.collection,
+ org.simantics.scl.runtime.either,
  org.simantics.scl.runtime.exceptions,
  org.simantics.scl.runtime.function,
  org.simantics.scl.runtime.io,
index eb1e8cd6ff35bed0186d22d68e7b115626246d04..818c5054307e95d5ad67bd3b211882fd9dc9befb 100644 (file)
@@ -1273,7 +1273,14 @@ The Either type represents values with two possibilities: a value of type `Eithe
 The `Either` type is sometimes used to represent a value which is either correct or an error; by convention, the `Left` constructor
 is used to hold an error value and the `Right` constructor is used to hold a correct value (mnemonic: "right" also means "correct").
 """
-data Either a b = Left a | Right b
+@JavaType "org.simantics.scl.runtime.either.Either"
+data Either a b =
+    @JavaType "org.simantics.scl.runtime.either.Left"
+    @FieldNames [value]
+    Left a
+  | @JavaType "org.simantics.scl.runtime.either.Right"
+    @FieldNames [value]
+    Right b
 
 deriving instance (Ord a, Ord b) => Ord (Either a b)
 deriving instance (Show a, Show b) => Show (Either a b)
diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Either.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Either.java
new file mode 100644 (file)
index 0000000..c792907
--- /dev/null
@@ -0,0 +1,4 @@
+package org.simantics.scl.runtime.either;
+
+public interface Either {
+}
diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Left.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Left.java
new file mode 100644 (file)
index 0000000..1e0c29b
--- /dev/null
@@ -0,0 +1,29 @@
+package org.simantics.scl.runtime.either;
+
+public class Left implements Either {
+    public final Object value;
+
+    public Left(Object value) {
+        this.value = value;
+    }
+    
+    @Override
+    public String toString() {
+        return "Left " + value;
+    }
+    
+    @Override
+    public boolean equals(Object obj) {
+        if(this == obj)
+            return true;
+        if(obj == null || obj.getClass() != getClass())
+            return false;
+        Right other = (Right)obj;
+        return value == null ? other.value == null : value.equals(other.value);
+    }
+    
+    @Override
+    public int hashCode() {
+        return 31 * (value == null ? 0 : value.hashCode()) + 13532;
+    }
+}
diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Right.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/either/Right.java
new file mode 100644 (file)
index 0000000..27aeec9
--- /dev/null
@@ -0,0 +1,29 @@
+package org.simantics.scl.runtime.either;
+
+public class Right implements Either {
+    public final Object value;
+
+    public Right(Object value) {
+        this.value = value;
+    }
+    
+    @Override
+    public String toString() {
+        return "Right " + value;
+    }
+    
+    @Override
+    public boolean equals(Object obj) {
+        if(this == obj)
+            return true;
+        if(obj == null || obj.getClass() != getClass())
+            return false;
+        Right other = (Right)obj;
+        return value == null ? other.value == null : value.equals(other.value);
+    }
+    
+    @Override
+    public int hashCode() {
+        return 31 * (value == null ? 0 : value.hashCode()) + 13533;
+    }
+}