[m-users.] switch confusion with cc_multi

Julian Fondren jfondren at minimaltype.com
Tue Aug 20 04:40:57 AEST 2019


On 2019-08-19 12:20, Zoltan Somogyi wrote:
> 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)
>     ).
> 

It didn't occur to me until just now that this might work:

   :- module semid2.
   :- interface.
   :- import_module io.
   :- pred main(io::di, io::uo) is det.
   :- implementation.

   :- type events
       --->    event_1
       ;       event_2
       ;       event_quit
       ;       event_x
       ;       event_y
       ;       event_z.

   main(!IO) :-
       handle(event_z, !IO).

   :- pred handle(events::in, io::di, io::uo) is det.
   handle(event_quit, !IO).
   handle(event_1, !IO).
   handle(event_2, !IO).
   handle(X, !IO) :-
       ( X = event_x ; X = event_y ; X = event_z ),
       io.write_string("events x, y and z not yet implemented\n", !IO).


More information about the users mailing list