]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/Prelude.scl
(refs #7433) Make Either type available in Java
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Prelude.scl
index c81f8308954e8ee241653b686b8d0bcbeffdaa6c..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)
@@ -1540,6 +1547,24 @@ joinWithSeparator :: Show a => String -> [a] -> String
 joinWithSeparator separator values = runProc ( 
     StringBuilder.toString $ printWithSeparator StringBuilder.new separator values)
 
+
+intercalate :: String -> [String] -> String
+intercalate separator strings = do
+    l = length strings
+    if l == 0
+    then ""
+    else if l == 1
+    then strings!0
+    else runProc do
+        sb = StringBuilder.new
+        sb << strings!0
+        loop i | i == l = ()
+               | otherwise = do
+            sb << separator << strings!i
+            loop (i+1)
+        loop 1
+        StringBuilder.toString sb
+
 instance (Show a) => Show [a] where
     sb <+ l = do 
         len = length l