Oberon Community Platform Forum

Development => ETH Zonnon => Topic started by: hreba on February 11, 2009, 06:28:49 PM



Title: Extensible recursive type?
Post by: hreba on February 11, 2009, 06:28:49 PM
My question is how to construct a recursive, extensible type in Zonnon, e.g. a list. The base type would be something like
Code:
type {ref} Item = object
  var {public} next: Item;
end Item;
'Object' is the only possibility here, a definition or implementation clause instead yield the error message Name 'Item' does not denote a type as expected. Now how do you extend it? Something like
Code:
type {ref} IntItem = object
  import Item;
  var {public} value: integer;
end;
does not work, because a variable of type Item cannot assume the dynamic type IntItem.
So what would be the Zonnon translation of the following Oberon types?
Code:
TYPE

  Item* = POINTER TO ItemD;
  ItemD* = RECORD
    next*: Item;
  END;

  IntItem* = POINTER TO IntItemD;
  IntItemD = RECORD (Item)
    value*: INTEGER;
  END;

In oo2c you have the additional possibility to mark Item as ABSTRACT, making it a recursive extensible abstract base class.


Title: Re: Extensible recursive type?
Post by: rmitin on February 16, 2009, 02:31:04 PM
Probably you should do something like this (if I understood the question right)
Code:
module Main;

definition Item;
var next: object{Item};
end Item;

procedure AddToTheList(root, newItem: object{Item});
var it: object{Item};
begin
 it := root;
 while it.next # nil do it := it.next end;
 it.next := newItem;
end AddToTheList;

procedure PrintList(root: IntItem);
var it: IntItem;
begin
 it := root;
 while it # nil do it.Print; it := IntItem(it.next) end;
end PrintList;


type {ref} IntItem = object implements Item
var {public} value: integer;

procedure {public} Print;
begin
writeln(value);
end Print;
end IntItem;

type

var root, item: IntItem;
i: integer;
begin
root:= new IntItem;
root.value := 0;
for i:= 1 to 10 do
item := new IntItem;
item.value := i;
AddToTheList(root, item)
end;
PrintList(root);
readln
end Main.


Title: Re: Extensible recursive type?
Post by: hreba on February 16, 2009, 05:03:43 PM
Thanks, this is exactly what I was looking for.


Title: Re: Extensible recursive type?
Post by: hreba on February 20, 2009, 04:17:44 PM
Hi Roman,

I stripped down your example to the absolute minimum:
Code:
module Main;

definition Item;
var next: object{Item};
end Item;

type {ref} IntItem = object implements Item
var {public} value: integer;
end IntItem;

begin
end Main.
and it works. However when I split it into a dll
Code:
module lib;

definition Item;
var next: object{Item};
end Item;

end lib.
and a client
Code:
module main;

import lib;

type {ref} IntItem = object implements lib.Item
var {public} value: integer;
end IntItem;

begin
end main.
I get the error message
'Zonnon.main+IntItem' does not implement interface member 'Zonnon.lib+Item.next.get'
In a similar case in the 'refines' thread you answered that it was meant to be a warning instead of an error and even this would be removed in the next revision. What is the case here?


Title: Re: Extensible recursive type?
Post by: rmitin on February 23, 2009, 09:33:04 AM
Thank you. This seems to be an error in the compiler. I'll fix that.


Title: Re: Extensible recursive type?
Post by: fitzer on June 04, 2011, 04:16:05 PM
I have been playing with this recursive list, and am trying to prepend to the list, but can't seem to get it to work.

Here is the code:

procedure PrependToList( root, newItem: object[Item} );
var
   it, rt: object{Item};

begin
it := newItem;
it.next := root;
root := it;
end PrependToList;

What am I missing here?

TIA,
Fitz


Title: Re: Extensible recursive type?
Post by: cfbsoftware on June 05, 2011, 08:39:45 AM
Try this:

Code:
procedure PrependToList( var root: IntItem; newItem: object{Item} );
var
  it: object{Item};
begin
  it := newItem;
  it.next := root;
  root := IntItem(it)
end PrependToList;


Title: Re: Extensible recursive type?
Post by: fitzer on June 06, 2011, 05:19:10 PM
Chris,

This is exactly what I needed.

Thanks,
Fitz