[m-users.] switch confusion with cc_multi

emacstheviking objitsu at gmail.com
Mon Aug 19 23:17:31 AEST 2019


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 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.
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!

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

I am probably doing this all the wrong way!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20190819/31506c84/attachment.html>


More information about the users mailing list