Type Extensions Across Modules
Posted: Fri Apr 12, 2019 2:56 am
Let's assume we have this module as starting point:
Now I want to extend the type as follows:
The compiler flags the assigment in 'P2' as error (undef). Of course I can export 'i' in M1, but that would be contrary to the concepts of encapsulation andprotected modularity. Also, if I am not the author of M1, and/or don't have the source code, this is not even an option.
If I put all the code into one module...
... the compiler happily accepts the assignment, ie. it "knows" that 'i' is an element of the extended type.
To me, 1) this does not seem consistent, and, 2) in the case of the separated modules, is a real limitation. The author of a module should not need to "prepare" possible future extensions, aside from the problems with exporting implementation details of a type.
Is this a limitation of the language or the compiler? Or do I miss something here?
(FWIW, the practical use case here is a serial USART module that I want to extend with a data transmission functionality using a buffer and interrupt-driven sending.)
Code: Select all
MODULE M1;
TYPE
T1* = POINTER TO T1Desc;
T1Desc* = RECORD
i: INTEGER
END;
PROCEDURE P1*(t: T1);
END P1;
PROCEDURE Init*(t: T1);
BEGIN
t.i := 13
END Init;
END M1.
Code: Select all
MODULE M2;
IMPORT M1;
TYPE
T2* = POINTER TO T2Desc;
T2Desc* = RECORD(M1.T1Desc)
j: INTEGER
END;
PROCEDURE P2*(t: T2);
BEGIN
t.i := 4 (*problem*)
END P2;
PROCEDURE Init*(t: T2);
BEGIN
t.j := 17
END Init;
END M2.
If I put all the code into one module...
Code: Select all
MODULE M3;
TYPE
T1* = POINTER TO T1Desc;
T1Desc* = RECORD
i: INTEGER
END;
T2* = POINTER TO T2Desc;
T2Desc* = RECORD(T1Desc)
j: INTEGER
END;
PROCEDURE P1*(t: T1);
END P1;
PROCEDURE P2*(t: T2);
BEGIN
t.i := 4
END P2;
PROCEDURE InitT1*(t: T1);
BEGIN
t.i := 13
END InitT1;
PROCEDURE InitT2*(t: T2);
BEGIN
t.j := 17
END InitT2;
END M3.
To me, 1) this does not seem consistent, and, 2) in the case of the separated modules, is a real limitation. The author of a module should not need to "prepare" possible future extensions, aside from the problems with exporting implementation details of a type.
Is this a limitation of the language or the compiler? Or do I miss something here?
(FWIW, the practical use case here is a serial USART module that I want to extend with a data transmission functionality using a buffer and interrupt-driven sending.)