[Oberon] Class Methods Vs. Procedure variables in Records

Richard Hable informujo at aon.at
Tue Feb 28 20:23:00 CET 2017


Am 2017-02-27 um 15:05 schrieb Andreas Pirklbauer:

> Wirth remains: these languages are still too complex. Why again are
> there separate concepts for structs (=records), classes (=pointers to
> records), modules, and protocols, and extensions, and and .. in Swift,
> when exactly two would do (records for data structuring and modules for
> information hiding)?

On the other hand, if you use Oberon or Oberon-2, you need at least
three definitions in order to define a simple class: the module
containing the class, a record type containing its member variables, and
a pointer type referring to the record.

And if you want dynamic method calls in Oberon according to the "do"
pattern, you need to define an additional method record type and a
pointer type referring to it; plus some helper variables and methods to
initialize everything.

Then, a simple Java source file like this:

class User {
	public Date birthDate;
	public int currentAge(){
		...
	}
}

becomes something like this:

MODULE Users;

TYPE
	User* = POINTER TO UserDesc;
	UserDesc* = RECORD
		birthDate*: Date;
		do*: UserMethod;
	END;
	
	UserMethod* = POINTER TO UserMethodDesc;
	UserMethodDesc* = RECORD
		currentAge*: PROCEDURE(): INTEGER;
	END;

VAR
	userMethod: UserMethod;

PROCEDURE CurrentAge(): INTEGER;
	BEGIN
		...
	END CurrentAge;

PROCEDURE NewUser*(): User;
	VAR
		user: User;
	BEGIN
		NEW (user);
		user.do := userMethod;
		RETURN user;
	END NewUser;

BEGIN
	NEW (userMethod);
	userMethod.currentAge := CurrentAge;
END Users.

(all syntax from memory)

Richard




More information about the Oberon mailing list