[m-rev.] diff: optimise TypeInfo_Struct.maybe_new

Peter Wang novalazy at gmail.com
Fri Oct 8 14:29:56 AEDT 2010


Branches: main

java/runtime/TypeInfo_Struct.java:
runtime/mercury_dotnet.cs.in:
        Add overloads of `TypeInfo_Struct.maybe_new' so the C# and Java
        compilers can pick the more appropriate version at compile time,
        as they know the type of the argument.

        Simplify the code.

diff --git a/java/runtime/TypeInfo_Struct.java b/java/runtime/TypeInfo_Struct.java
index bae46b3..a51b366 100644
--- a/java/runtime/TypeInfo_Struct.java
+++ b/java/runtime/TypeInfo_Struct.java
@@ -23,21 +23,22 @@ public class TypeInfo_Struct extends PseudoTypeInfo
 		sanity_check();
 	}
 
-	public static TypeInfo_Struct maybe_new(final Object obj)
+	public static TypeInfo_Struct maybe_new(final TypeInfo_Struct ti)
+	{
+		return ti;
+	}
+
+	public static TypeInfo_Struct maybe_new(final TypeCtorInfo_Struct tci)
+	{
+		return new TypeInfo_Struct(tci);
+	}
+
+	public static TypeInfo_Struct maybe_new(final PseudoTypeInfo obj)
 	{
-		// In at least one place in the standard library we make up a
-		// TypeInfo out of thin air to satisfy the compiler.
-		if (obj == null) {
-			return null;
-		}
 		if (obj instanceof TypeCtorInfo_Struct) {
 			return new TypeInfo_Struct((TypeCtorInfo_Struct) obj);
 		}
-		if (obj instanceof TypeInfo_Struct) {
-			return (TypeInfo_Struct) obj;
-		}
-		throw new java.lang.Error(
-			"expected TypeInfo_Struct or TypeCtorInfo_Struct");
+		return (TypeInfo_Struct) obj;
 	}
 
 	public void init(TypeCtorInfo_Struct tc, PseudoTypeInfo[] as)
diff --git a/runtime/mercury_dotnet.cs.in b/runtime/mercury_dotnet.cs.in
index 407db44..45ef6c4 100644
--- a/runtime/mercury_dotnet.cs.in
+++ b/runtime/mercury_dotnet.cs.in
@@ -242,20 +242,20 @@ public class TypeInfo_Struct : PseudoTypeInfo {
         init(ti.type_ctor, args.Length, args);
     }
 
-    public static TypeInfo_Struct maybe_new(object obj) {
-        if (obj == null) {
-            return null;
-        }
+    public static TypeInfo_Struct maybe_new(TypeInfo_Struct ti) {
+        return ti;
+    }
+
+    public static TypeInfo_Struct maybe_new(TypeCtorInfo_Struct tc) {
+        return new TypeInfo_Struct(tc);
+    }
+
+    public static TypeInfo_Struct maybe_new(PseudoTypeInfo obj) {
         TypeCtorInfo_Struct tc = obj as TypeCtorInfo_Struct;
         if (tc != null) {
             return new TypeInfo_Struct(tc);
         }
-        TypeInfo_Struct ti = obj as TypeInfo_Struct;
-        if (ti != null) {
-            return ti;
-        }
-        // XXX throw exception
-        return null;
+        return (TypeInfo_Struct) obj;
     }
 
     public void init(TypeCtorInfo_Struct tc, PseudoTypeInfo[] args) {

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list