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

Peter Wang novalazy at gmail.com
Tue Sep 22 16:40:56 AEST 2009


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.

> +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.

Otherwise, looks fine.

Peter
--------------------------------------------------------------------------
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