[m-rev.] for review: implement equals method for Mercury enumerations

Ian MacLarty maclarty at csse.unimelb.edu.au
Tue Sep 22 17:12:02 AEST 2009


On Tue, Sep 22, 2009 at 04:40:56PM +1000, Peter Wang wrote:
> On 2009-09-22, Ian MacLarty <maclarty at csse.unimelb.edu.au> wrote:
> > For review by Peter.
> > 
> > Estimated hours taken: 2
> > Branches: main
> > 
> > Override Object#equals for Mercury enumerations in the Java grades
> > and document that this should be used for equality testing in Java
> > code.  The problem with using == is that instances might not have
> > the same addresses if they are serialized and then deserialized.
> 
> > diff -u -r1.141 mlds_to_java.m
> > --- compiler/mlds_to_java.m	21 Sep 2009 04:08:57 -0000	1.141
> > +++ compiler/mlds_to_java.m	22 Sep 2009 05:52:36 -0000
> > @@ -2795,10 +2790,20 @@
> >          unexpected(this_file, "output_type: erlang foreign_type")
> >      ).
> >  output_type(_, mlds_class_type(Name, Arity, _ClassKind), !IO) :-
> > -    % We used to treat enumerations specially here, outputting
> > -    % them as "int", but now we do the same for all classes.
> > -    output_fully_qualified_thing(Name, output_class_name, !IO),
> > -    io.format("_%d", [i(Arity)], !IO).
> > +    (
> > +        Name = qual(ModuleName, _, ClassName),
> > +        SymName = mlds_module_name_to_sym_name(ModuleName),
> > +        qualified(unqualified("jmercury"), "runtime") = SymName
> 
> Use java_names.mercury_runtime_package_name.
> 

Done.

> > +public abstract class MercuryEnum {
> > +    public final int MR_value;
> > +
> > +    protected MercuryEnum(int val) {
> > +        MR_value = val;
> > +    }
> > +
> > +    @Override 
> > +    public boolean equals(Object o) {
> > +        return ((MercuryEnum)o).MR_value == this.MR_value;
> > +    }
> >  }
> 
> I discussed this part with Ian offline.
> 

Here is the interdiff:

diff -u compiler/mlds_to_java.m compiler/mlds_to_java.m
--- compiler/mlds_to_java.m	22 Sep 2009 05:52:36 -0000
+++ compiler/mlds_to_java.m	22 Sep 2009 07:09:07 -0000
@@ -2793,7 +2793,7 @@
     (
         Name = qual(ModuleName, _, ClassName),
         SymName = mlds_module_name_to_sym_name(ModuleName),
-        qualified(unqualified("jmercury"), "runtime") = SymName
+        SymName = mercury_runtime_package_name
     ->
         % Don't mangle runtime class names.
         io.write_string("jmercury.runtime.", !IO),
diff -u java/runtime/MercuryEnum.java java/runtime/MercuryEnum.java
--- java/runtime/MercuryEnum.java	22 Sep 2009 05:52:41 -0000
+++ java/runtime/MercuryEnum.java	22 Sep 2009 07:09:11 -0000
@@ -19,5 +19,11 @@
     @Override 
     public boolean equals(Object o) {
-        return ((MercuryEnum)o).MR_value == this.MR_value;
+        return o != null && o instanceof MercuryEnum &&
+            ((MercuryEnum)o).MR_value == this.MR_value;
+    }
+
+    @Override
+    public int hashCode() {
+        return MR_value;
     }
 }
--------------------------------------------------------------------------
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