Opaque Types in Modula-2

My knowledge of Modula-2 is limited to having read Programming in Modula-2 and having written a few toy programs.

I'm a little confused about opaque types in Modula-2. They are mentioned in passing in Programming in Modula-2, 4E, Chapter 24, "Definition and Implementation Parts", on page 83. I thought that to make an opaque type, you declared a type in the definition module as

TYPE Opaque;

and then in the implementation module you finish that declaration as

TYPE Opaque = POINTER TO Internal;
     Internal = RECORD
          i: INTEGER;
     END;

with a pointer to an internal type that is not mentioned in the definition module, and which is thus not visible to clients of the module.

However, that doesn't seem to work, in the example I wrote and compiled. I'm using GNU Modula-2 with the PIM4 libraries (it's nice that GM2 is in the standard Fedora repository now, no doubt helped by GM2 being officially accepted into gcc).

Here's Opaque.def:

DEFINITION MODULE Opaque;
TYPE Opaque;
PROCEDURE Make (VAR o: Opaque; i : INTEGER);
PROCEDURE Set (VAR o: Opaque; i: INTEGER);
PROCEDURE Get (o: Opaque) : INTEGER;
END Opaque.

And here's Opaque.mod:

IMPLEMENTATION MODULE Opaque;
FROM Storage IMPORT ALLOCATE, DEALLOCATE;

TYPE Opaque = POINTER TO Internal;
     Internal = RECORD
          i: INTEGER;
     END;

PROCEDURE Make (VAR o: Opaque; i: INTEGER);
BEGIN
     ALLOCATE (o, SIZE (Opaque));
     o^.i := 1;
END Make;

PROCEDURE Set (VAR o: Opaque; i: INTEGER);
BEGIN
     o^.i := i;
END Set;

PROCEDURE Get (o: Opaque) : INTEGER;
BEGIN
     RETURN o^.i;
END Get;

END Opaque.

And here is the test program:

MODULE TryOpaque;
FROM InOut IMPORT WriteString, WriteLn, WriteInt;
FROM Opaque IMPORT Opaque, Make, Set, Get;

VAR
     o: Opaque;
     i: INTEGER;

BEGIN
     WriteString ("TryOpaque"); WriteLn;
     Make (o, 10);
     Set (o, 5);
     i := Get (o);
     WriteInt (i, 0); WriteLn;
     (* Hmm.  I thought this was supposed to illegal. *)
     o^.i := 100;
     WriteInt (o^.i, 0); WriteLn;
END TryOpaque.

I had expected that the compiler would refuse to compile the references to "o^.i".

Could anyone clear up my confusion?

=> Posted in: s/pascal | πŸ‘½ TKurtBond

2024-12-24 Β· 4 weeks ago Β· πŸ‘ norayr

4 Comments ↓

=> πŸ™ norayr [mod] Β· Dec 30 at 00:17:

dsar, who is an expert in m2 said about your post:

By the way, that code shouldn't compile, you can't access members of an opaque type

=> πŸ‘½ TKurtBond [OP] Β· Dec 31 at 22:46:

And that’s what I thought, too. Maybe a bug in GNU Modula-2?

=> πŸ™ norayr [mod] Β· Jan 06 at 01:14:

it's possible. if you write in gm2 mailing list, gaius will respond.

=> πŸ‘½ TKurtBond [OP] Β· Jan 15 at 20:32:

Emailed gm2 mailing list.

=> β€” Error with Opaque types in GNU Modula-2?

Proxy Information
Original URL
gemini://bbs.geminispace.org/s/pascal/23166
Status Code
Success (20)
Meta
text/gemini; charset=utf-8
Capsule Response Time
86.339961 milliseconds
Gemini-to-HTML Time
0.940893 milliseconds

This content has been proxied by September (ba2dc).