Algol 68 - feed base-type objects to union-type structure
From
Janis Papanagnou@janis_papanagnou+ng@hotmail.com to
comp.lang.misc on Mon Aug 24 23:34:54 2026
From Newsgroup: comp.lang.misc
In an attempt to create a program relying on generalization using
Algol68's UNIONs I hit a hindrance.
Reading in the Genie manual
"This means that operators which accept operands with united modes
will also accept operands whose modes are any of the constituent
modes."
made me cocky, and I tried the variants following to the subsequent
test frame.
BEGIN
MODE U = UNION ( INT, CHAR, STRING ); # supported base-types #
MODE O = STRUCT ( STRING n, REF U v); # contains a 'REF' var #
MODE OS = FLEX [1:0] O;
CO *** here go the variants 1-4 subsequently discussed *** CO
OS olist = ( ("a", a), ("b", b), ("c", c), ("d", d) ); # e.g. #
PROC op = (OS olist) VOID : SKIP; # complex proc-body omitted #
op (olist)
END
My original intention was to use variant #1#, letting the various
variables _get filled by the procedure 'op'_.
#1# INT a; CHAR b; CHAR c; STRING d;
This lead to an error with the used Genie compiler.[*] Initializing
the values as follows #2# consequently doesn't change anything here.
#2# INT a := 1; CHAR b := "*"; CHAR c := "@"; STRING d := "";
(So the above "operands whose modes are any of the constituent modes"
obviously doesn't apply here.)
That these variants don't work is a pity since I intended to have the
_basic types_ (INT, CHAR, etc.) visible at the variable declarations,
and not their _union-type_ 'U'.
Okay, biting the bullet, I then used the generic union-type instead
to declare the variables, variant #3#.
#3# U a; U b; U c; U d;
which worked in the (reduced) code-frame above. But the problem with
that is that the _types_ of a, b, c, d are *unknown* at that stage.
So when using a generic parse function in 'PROC op' - in this case
I'm using Genie's 'PROC gets' - it (rightly) complains at that point
because of the unknown types of the involved variables.
So the final form became variant #4#, where the types are deduced from
the initialized declaration. (The types are visibly harder to see!)
#4# U a := 1; U b := "*"; U c := "@"; U d := "";
Now originally intending #1# and finally reaching #4# I wonder whether
I've missed anything to keep the base-types of the variable declaration
intact and not obfuscating their types by the need of using MODE U for
their declaration. - Any ideas?
Janis
[*] (CHAR, REF INT) and (CHAR, REF CHAR) and (CHAR, REF CHAR) and
(CHAR, REF STRING) cannot be coerced to O in a strong-unit, in
closed-clause
--- Synchronet 3.22a-Linux NewsLink 1.2