[Oberon] Alternatives for cases.

peter at easthope.ca peter at easthope.ca
Wed Mar 6 20:49:36 CET 2019


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


More information about the Oberon mailing list