Oberon Community Platform Forum
November 23, 2017, 09:41:50 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Extensible recursive type?  (Read 8737 times)
hreba
Newbie
*
Posts: 14


« 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.
Logged
rmitin
Moderator
Newbie
*****
Posts: 22


« Reply #1 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.
Logged
hreba
Newbie
*
Posts: 14


« Reply #2 on: February 16, 2009, 05:03:43 PM »

Thanks, this is exactly what I was looking for.
Logged
hreba
Newbie
*
Posts: 14


« Reply #3 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?
Logged
rmitin
Moderator
Newbie
*****
Posts: 22


« Reply #4 on: February 23, 2009, 09:33:04 AM »

Thank you. This seems to be an error in the compiler. I'll fix that.
Logged
fitzer
Newbie
*
Posts: 2


« Reply #5 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
Logged
cfbsoftware
Full Member
***
Posts: 106


WWW
« Reply #6 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;
« Last Edit: June 05, 2011, 08:41:44 AM by cfbsoftware » Logged

Chris Burrows
Astrobe v6.0 (Jun 2016): Oberon for ARM Cortex-M4 and Cortex-M3 Microcontrollers
http://www.astrobe.com
fitzer
Newbie
*
Posts: 2


« Reply #7 on: June 06, 2011, 05:19:10 PM »

Chris,

This is exactly what I needed.

Thanks,
Fitz
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!