[Oberon] Alternatives for cases.

Jörg Straube joerg.straube at iaeth.ch
Thu Mar 7 07:45:28 CET 2019


Lyall

your „case[choice]()“ is basically what a „CASE choice OF“ does. So there is no clear runtime benefit of your aporoach vs. CASE.

But you have to declare PROCEDUREs that is not needed in case of CASE. A procedure generates quite some overhead during BEGIN and END.
So the runtime behaviour of the jump as such is comparable to CASE but the code you jump to takes longer in your approach compared to CASE.

br
Jörg

> Am 06.03.2019 um 20:49 schrieb <peter at easthope.ca> <peter at easthope.ca>:
> 
> Hi,
> 
> Seeing as alternative techniques for exiting a procedure and for ending 
> interation are popular subjects recently, another question about 
> alternatives.  
> 
> Elimination of IF and CASE has some aesthetic appeal.
> Otherwise, is the array technique ever worth using?  
> OK when most elements in the array are distinct?
> How fast is array indexing compared to jumping?
> How bad is the additional procedure call?
> 
> Thanks for comments,               ... Lyall E.
> 
> ==============================
> MODULE Cases IN Oberon;
>    IMPORT Texts, Oberon;
>    VAR 
>        choice: INTEGER;
>        case: ARRAY 3 OF PROCEDURE;
>        W: Texts.Writer;
> 
>    PROCEDURE Case0();
>    BEGIN
>        Texts.WriteString(W, "Case 0"); Texts.WriteLn(W)
>    END Case0;
> 
>    PROCEDURE Case1();
>    BEGIN
>        Texts.WriteString(W, "Case 1"); Texts.WriteLn(W)
>    END Case1;
> 
>    PROCEDURE Case2();
>    BEGIN
>        Texts.WriteString(W, "Case 2"); Texts.WriteLn(W)
>    END Case2;
> 
>    PROCEDURE Test*();
>    BEGIN
>        choice := 1;
>        CASE choice OF
>             0: Texts.WriteString(W, "CASE 0"); Texts.WriteLn(W)
>            |1: Texts.WriteString(W, "CASE 1"); Texts.WriteLn(W)
>            |2: Texts.WriteString(W, "CASE 2"); Texts.WriteLn(W)
>        END;
> 
>        case[0] := Case0;
>        case[1] := Case1;
>        case[2] := Case2;
>        choice := 2;
>        case[choice]();
>        Texts.Append(Oberon.Log, W.buf) 
>    END Test;
> 
> BEGIN
>    Texts.OpenWriter(W)
> END Cases.
> 
> Cases.Test
> 
> 
> -- 
> Message composed and transmitted by software designed to avoid the 
> complication and vulnerability of antivirus software.
> 
> 123456789 123456789 123456789 123456789 123456789 123456789 123456789
> Pender Is., BC: +1 604 670 0140     Washington State: +1 360 639 0202
> http://easthope.ca/Peter.html              Bcc: peter at easthope. ca
> --
> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> https://lists.inf.ethz.ch/mailman/listinfo/oberon



More information about the Oberon mailing list