[m-rev.] for review: Make subtypes inherit user-defined equality/comparison from base type.

Julien Fischer jfischer at opturion.com
Mon Apr 12 17:14:21 AEST 2021


Hi Peter,

On Mon, 12 Apr 2021, Peter Wang wrote:

> Since values of a subtype are tested for equality or compared
> by first converting to the base type then testing/comparing,
> it means that subtypes implicitly inherited any user-defined
> equality/comparison from the base type. Allowing a subtype to also
> define its own equality/comparison predicates seems more confusing
> than useful, so disallow it.

...

> diff --git a/doc/reference_manual.texi b/doc/reference_manual.texi
> index 2615b2be3..3f8db1298 100644
> --- a/doc/reference_manual.texi
> +++ b/doc/reference_manual.texi
> @@ -2649,6 +2649,10 @@ Example:
>     --->    [T | list(T)].
> @end example
> 
> +Subtypes may not have user-defined equality or comparison predicates.

s/may/must/

> +The base type of a subtype may have user-defined equality or comparison,

Make that a separate sentence.

> +in which case values of the subtype will be compared using those predicates.

    In that case, values of the subtype will be tested for equality or
    or compared using those predicates.

...

> diff --git a/tests/invalid/subtype_user_compare.err_exp b/tests/invalid/subtype_user_compare.err_exp
> new file mode 100644
> index 000000000..c2dc66217
> --- /dev/null
> +++ b/tests/invalid/subtype_user_compare.err_exp
> @@ -0,0 +1,2 @@
> +subtype_user_compare.m:015: Error: the subtype `citrus'/0 is not allowed to
> +subtype_user_compare.m:015:   have user-defined equality or comparison.

I wonder whether it's worth providing a some extra explanation in the
verbose form of this message, something like:

   Only base types are allowed to have user-defined equality or comparison.

That looks fine otherwise.

Julien.


More information about the reviews mailing list