[Oberon] CASE without ELSE

Jörg joerg.straube at iaeth.ch
Sat Feb 17 23:52:36 CET 2018


The difference between IF and CASE is, that in the CASE the type of the expression is changed to the type extension. Very handy for message handlers. See below

TYPE
  Base = RECORD END
  Ext = RECORD (Base) i: INTEGER END;
  Ext2 = RECORD (Base) c: CHAR END;

PROCEDURE CaseExample(VAR b: Base);
  BEGIN
    CASE b OF
    Ext:      b.i := 0
    | Ext2: b.c := „A“
    END;
    (* this is equivalent *)
    IF b IS Ext THEN b(Ext).i := 0
    ELSIF b is Ext2 THEN b(Ext2).c := „A“
    END
  END CaseExample;

Jörg

> Am 17.02.2018 um 23:32 schrieb Arthur Yefimov <artur.efimov at gmail.com>:
> 
> Andreas wrote:​
> ​> ​One just needs to get the order right: extended types first, base type last (if one placed the base type at the beginning of the list, it would always evaluate to TRUE even if M were an extension of T)
> 
> ​Does it mean that CASE statement for pointer/record types work like an IF statement? Does it sequentially check all options one by one until it hits the right one? In this case, what does "CASE" give in comparison to using "IS" clause,​ i.e. "IF obj IS MyViewer THEN ... ELSIF obj IS ..."?
> 
> --
> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> https://lists.inf.ethz.ch/mailman/listinfo/oberon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.inf.ethz.ch/pipermail/oberon/attachments/20180217/8007b277/attachment.html>


More information about the Oberon mailing list