[m-rev.] for review: Announce subtypes and coerce.

Julien Fischer jfischer at opturion.com
Wed Apr 7 11:19:37 AEST 2021


Hi Peter,

On Tue, 6 Apr 2021, Peter Wang wrote:

> On Sun, 04 Apr 2021 16:36:10 +1000 Julien Fischer <jfischer at opturion.com> wrote:
>>
>> Hi Peter,
>>
>> On Thu, 1 Apr 2021, Peter Wang wrote:
>>
>>> NEWS:
>>>    As above.
>>>
>>> doc/reference_manual.texi:
>>>    Uncomment documentation for coerce.
>>>
>>> diff --git a/NEWS b/NEWS
>>> index 058e00662..ead462b39 100644
>>> --- a/NEWS
>>> +++ b/NEWS
>>> @@ -341,6 +341,9 @@ Changes to the Mercury standard library
>>> Changes to the Mercury language
>>> -------------------------------
>>>
>>> +* The type system now supports subtypes, which work in tandem with
>>> +  type conversion expressions ("coerce").
>>
>> I thinks this feature warrants a bit more of a description than one
>> sentence. In particular, the NEWS file should definitely contain an
>> example subtype.
>
> How about:
>
> diff --git a/NEWS b/NEWS
> index ead462b39..7bd2a2582 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -342,7 +342,21 @@ Changes to the Mercury language
> -------------------------------
>
> * The type system now supports subtypes, which work in tandem with
> -  type conversion expressions ("coerce").
> +  type conversion expressions ("coerce"). For example, the following
> +  defines a subtype `real_color` of a discriminated union type `color`:
> +
> +        :- type color
> +            --->    rgb(float, float, float)
> +            ;       cmyk(float, float, float, float)
> +            ;       named(string).
> +
> +        :- type real_color =< color
> +            --->    rgb(float, float, float)
> +            ;       cmyk(float, float, float, float).
> +
> +  A term of type `real_color` can be converted to a term of type `color`
> +  with `coerce(Term)`. A term of type `color` can be converted to a term of
> +  type `real_color` if it has an appropriate inst.

That's better.  The one other I would mention here is that types and
subtypes share their data representation, so coerce expressions do not
cost anything.

Julien.


More information about the reviews mailing list