[m-users.] switch confusion with cc_multi

Mark Brown mark at mercurylang.org
Tue Aug 20 01:23:09 AEST 2019


On Mon, Aug 19, 2019 at 11:18 PM emacstheviking <objitsu at gmail.com> wrote:
>
> OK, my "game" is progressing and I want to deal with the SDL events in the main event / render loop.
> The event loop checks for a single SDL event like so:
>
> %--------------------------------------------------------------------------
> %
> % Event Loop
> %
> :- pred event_loop(app_state::in, app_state::out, bool::out, io::di, io::uo) is det.
>
> event_loop(Ain, Aout, Quit, !IO) :-
> sdl_pollevent(MEv, !IO),
> (
> MEv = no,
> Quit = no,
> Ain = Aout
> ;
> MEv = yes(Event),
> (
> if Event = quit_event
> then Ain = Aout,
> Quit = yes
> else
> handle_event(Event, Ain, Aout, !IO),
> Quit = no
> )
> ).
>
> And here is the individual event processing.
>
> %--------------------------------------------------------------------------
> %
> % HandleA Single Event
> %
> :- pred handle_event(sdl_event::in, app_state::in, app_state::out, io::di, io::uo) is det.
>
> handle_event(E, A, A, !IO) :-
> ( if E = key_up(_A, _B) then
> format("keyup:\n", [], !IO)
>
> else if E = filedrop_event(F) then
> format("File drop: %s\n", [s(F)], !IO)
>
> % ... more if then else checks as events are added
> else
> format("dont care:\n", [], !IO)
> ).
>
> Every time I use multiple predicates with (_,..) as the event it says I have multiple solutions

Can we assume that you have user defined equality or comparison on
this type (and that it compiled ok until you tried to deconstruct one
of the values)?

> but then I tried to make handle_event cc_multi as there can be only one event at a time and it can only be one type of event.

Assuming the above, that's not quite the issue. The question is, given
that there is only one event _but that it can be represented in
multiple ways_, do you always produce the same output for all the
different representations?

Mark


More information about the users mailing list