[m-users.] Multi predicate of entire enum
Ace
sheganinans at gmail.com
Mon Jul 20 16:00:04 AEST 2020
Very nice Julien!
I just went ahead and went the code generation way. Already works, based
off of some code I wrote a while ago to do some Mercury codegen.
I like this solution a lot, very straightforward and does exactly what I
thought should be possible within type_desc and construct but wasn't sure
exactly how.
Thanks!
Aistis Raulinaitis
On Sun, Jul 19, 2020 at 10:27 PM Julien Fischer <jfischer at opturion.com>
wrote:
>
> Hi,
>
> On Sun, 19 Jul 2020, Ace wrote:
>
> > So say I have some enum:
> >
> > `:- type ex ---> a; b; c.`
> >
> > And I want to generate a function similar to:
> >
> > ```
> > :- pred ex_mul(ex::out) is multi.
> > ex_mul(a).
> > ex_mul(b).
> > ex_mul(c).
> > ```
> >
> > Any kind of standard library predicate I can use so I don't have to
> > add a new case to the predicate for every instance of the enum?
>
> You could use RTTI to implement such a predicate, although it will be more
> expensive than than the simple form above, e.g. something like:
>
> :- pred ex_mul(ex::out) is multi.
>
> ex_mul(V) :-
> TypeDesc = type_desc.type_of(V),
> NumFunctors = construct.det_num_functors(TypeDesc),
> ( if
> int.nondet_int_in_range(0, NumFunctors - 1, FunctorNum),
> Univ = construct.construct(TypeDesc, FunctorNum, [])
> then
> univ.det_univ_to_type(Univ, V)
> else
> error("should not get here")
> ).
>
> Julien.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20200719/53c738fd/attachment.html>
More information about the users
mailing list