• Variant records vs extensible records

    From trijezdci@21:1/5 to Martin Brown on Tue Aug 30 07:13:24 2016
    On Tuesday, 30 August 2016 18:22:01 UTC+9, Martin Brown wrote:

    There was also an ugly kludge in PIM M2 where you could do something
    of the form of a CASE BOOLEAN OF TRUE: | FALSE: END inside a RECORD
    to create a variant that could be interpreted different ways.

    That is a use case for variant records, which were replaced in R10 with extensible records.

    TYPE R = RECORD
    CASE BOOLEAN OF
    TRUE : foo : CARDINAL
    | FALSE : bar : INTEGER
    END
    END;

    could be modeled with extensible records as

    TYPE Base = RECORD ( NIL ) END;

    TYPE V1 = RECORD ( Base )
    foo : CARDINAL
    END;

    TYPE V2 = RECORD ( Base )
    bar : INTEGER
    END;

    You could then pass instances of either V1 or V2 to a formal parameter of type R but you can only access fields that are not part of R within a type guard.

    PROCEDURE Init ( VAR x : Base );
    BEGIN
    x.foo := 1; (* compile time error here *)
    CASE x OF
    V1 : x.foo := 1 (* alright here *)
    | V2 : x.bar := -1 (* alright here *)
    END
    END Init;

    We make use of this in our channel based IO library. It uses generic channel type which is an extensible record type.

    https://bitbucket.org/trijezdci/m2r10/src/tip/_STANDARD_LIBRARY/IOLIBRARY/ChanIO.Alt.def

    The actual channels are extensions of that and each channel implements its own channel specific details. From the user point of view it all looks like a single type with a single interface.

    ISO tried to accomplish the same outcome but without extensible record types at its disposal the whole thing became overly complex and has cyclic dependencies.


    I presume this is so far against the spirit of R10 that you don't
    even mention it.

    Well, the chart does say "removed" in the R10 column of the variant records row.


    The ugliness of this should still be recorded for posterity.

    :-)

    In Wirth's defense: It probably looked like a good idea at the time.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)