[m-users.] switch confusion with cc_multi

Zoltan Somogyi zoltan.somogyi at runbox.com
Tue Aug 20 03:20:19 AEST 2019



On Mon, 19 Aug 2019 14:17:31 +0100, emacstheviking <objitsu at gmail.com> wrote:
> Every time I use multiple predicates with (_,..) as the event

What does this mean? If you don't show us the actual code, we cannot tell you
what is wrong with it. We can only guess, as the previous two replies have done,
which may only confuse you further.

> I guess I am just confused trying to implement some kind of event handler
> that grows as I need it and doesn't want to have code for all possible
> eventualites right from the start as I don't know what events I want to
> process yet!

The obvious way to handle that is to start with a type that has one function
symbol for each of the events you know you need, with a complete switch
in the event loop on the values of that type. Then, as you find the need for
more kinds of events, add them to the type and to the switch at the same time.

The event loop should look something like this:

event_loop(!AppState, !IO) :-
    sdl_event(MaybeEvent, !IO),
    (
        MaybeEvent = no,
        Quit = no
    ;
        MaybeEvent = yes(Event),
        (
            Event = event_quit,
            Quit = yes
        ;
            Event = event_1(...),
            Quit = no,
            ... handle event 1 ...
        ;
            Event = event_2(...),
            Quit = no,
            ... handle event 2 ...
        ;
            ( Event = event_x(...)
            ; Event = event_y(...)
            ; Event = event_z(...)
            ),
            Quit = no,
            io.format("events x, y and z not yet implemented", !IO)
        ),
    ),
    (
        Quit = yes
    ;
        Quit = no,
        event_loop(!AppState, !IO)
    ).

(Unlike yours, this event_loop predicate does in fact loop.)

> So, is the if-then-else the only way of creating a "det" predicate that
> effectively swallows the multiple solutions into a deterministic bucket ?

"the multiple solutions" of what? Each piece of code processing one particular kind
of event should have exactly one solution, since only det code may do I/O.

Zoltan.


More information about the users mailing list