AW: [Oberon] ARRAY OF CHAR mystery

Stauber Sven Philipp sven.stauber at inf.ethz.ch
Tue Jun 22 16:47:18 MEST 2010


Hi,

pchars^ := chars; won't work because type compatibility in Oberon languages (at least in those I've seen) is not a result of "structure" equivalence.

e.g.

VAR
   A : ARRAY 4 OF CHAR;
   B : ARRAY 4 OF CHAR;
BEGIN
   A := B; (* won't work; the 'two' types ARRAY 4 OF CHAR are treated as being separate types *)

To make it work, either declare the variables using one type only:

VAR
   A, B : ARRAY 4 OF CHAR;
BEGIN
   A := B; (* works because same type *)

Or introduce a new type so you can use it in more than one variable declaration:

TYPE
   Array4OfChar : ARRAY 4 OF CHAR;
VAR
   A : Array4OfChar;
   B : Array4OfChar;
BEGIN
   A := B; (* works *)

Or, for ARRAY OF CHAR, use the built-in COPY statement:

VAR
   A : ARRAY 4 OF CHAR;
   B : ARRAY 300 OF CHAR;
BEGIN
   COPY(B, A); (* works, will copy the first 3 characters only. A[3] will be 0X *)

For your example, you could use:

COPY(pchars^, chars); (* warning: possible truncation, but at least, chars will be 0X-terminated *)

Note:

TYPE
   String : ARRAY 32 OF CHAR;
   StringPtr : POINTER TO String;
VAR
   A : String; B : StringPtr;
BEGIN
   NEW(B); a := b^; (* should work *)

Best,
Sven

-----Ursprüngliche Nachricht-----
Von: oberon-bounces at lists.inf.ethz.ch [mailto:oberon-bounces at lists.inf.ethz.ch] Im Auftrag von spir
Gesendet: Dienstag, 22. Juni 2010 16:05
An: oberon
Betreff: [Oberon] ARRAY OF CHAR mystery

Hello,

In an attempt to build a string abstraction as evoked in the previous theread, I'm blocked with the following issue:

I find myself unable to assign an ARRAY OF CHAR value to an ARRAY OF CHAR variable. Below pchars is of type POINTER TO ARRAY OF CHAR. The statement
    pchars^ := chars;
is refused. Below results of compilation with oo2c and obc:

oo2c --make "trial.Mod" (in directory: /home/spir/prog/oberon)
Compilation failed.
/home/spir/prog/oberon/trial.Mod:16:15: Expression not compatible with variable type `ARRAY'

obc  "trial.m" (in directory: /home/spir/prog/oberon)
Compilation failed.
"trial.m", line 16: type ARRAY OF CHAR is needed on the RHS of this assignment
> 	pchars^ := chars;
> 	         ^^^^^
> This expression has type ARRAY OF CHAR

obc's message is very nice ;-)



A side issue is that I cannot find another way to return an array or a record other than via a pointer. Meaning eg a Text type would actually need to be a type of pointers to actual string data. But pointers break clear and secure value semantics, in that after
	t2 := t1;
any change to either text affects both. One is thus forced to explicitely dereference all string variables:
	t2^ := t1^;

I also find myself forced to build the string data self as a record to be able to affect it various needed methods. So, I end up as of now with the following:

TYPE
	Chars	= ARRAY OF CHAR;
	PChars	= POINTER TO Chars;
	DText	= RECORD		(* actual text data *)
		pchars	: PChars;
		count	: LONGINT;
		isEmpty	: BOOLEAN;
	END;
	Text	= POINTER TO DText;

But I'm blocked, as explained above, in assigning pchars^ at creation of a Text.


denis
________________________________

vit esse estrany ☣

spir.wikidot.com
--
Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
https://lists.inf.ethz.ch/mailman/listinfo/oberon


More information about the Oberon mailing list