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 ..."?
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@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@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
On Sat, Feb 17, 2018, at 11:32 PM, Arthur Yefimov wrote:
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 ..."?
Yes, it is a syntactic sugar for IF/ELSIF and type guard, nothing more. This is the reason I don't like the Oberon-2 WITH statement merged in CASE statement since they use two different implementations that lead to confusion (just check recent threads in comp.lang.oberon). Regarding the possibility of implementing a jump table for typecase, it's not possible due to type extension, you have to know a priori all the hierarchy. This is only possible with final types that Oberon and Oberon-2 don't have (but Component Pascal does have). If you are interested, this is further discussed in Appel - Modern compiler implementation (in Java) on page 291.
participants (3)
-
Arthur Yefimov -
Diego Sardina -
Jörg