]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/SafeDynamic.scl
(refs #7767) SafeDynamic module
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / SafeDynamic.scl
diff --git a/bundles/org.simantics.scl.runtime/scl/SafeDynamic.scl b/bundles/org.simantics.scl.runtime/scl/SafeDynamic.scl
new file mode 100644 (file)
index 0000000..b113f8a
--- /dev/null
@@ -0,0 +1,22 @@
+import "Prelude"
+import "JavaBuiltin" as Java
+
+@JavaType "org.simantics.scl.compiler.dynamic.SafeDynamic"
+data SafeDynamic =
+    @FieldNames [type_, value]
+    SafeDynamic Type Dynamic
+
+toSafeDynamic :: Typeable a => a -> SafeDynamic
+toSafeDynamic val = SafeDynamic (typeOf val) (toDynamic val)
+
+fromSafeDynamic :: Typeable a => SafeDynamic -> Maybe a
+fromSafeDynamic (SafeDynamic type_ value) =
+    if type_ == typeOfProxy (TypeProxy :: TypeProxy a)
+    then Just (Java.unsafeCoerce value)
+    else Nothing
+
+forgetType :: SafeDynamic -> Dynamic
+forgetType (SafeDynamic _ value) = value
+
+typeOfSafeDynamic :: SafeDynamic -> Type
+typeOfSafeDynamic (SafeDynamic t _) = t