From: Lutz Donnerhacke <lutz@iks-jena.de>
Subject: Re: Indexed list
Date: Fri, 15 Aug 2003 13:01:31 +0000 (UTC)
Date: 2003-08-15T13:01:31+00:00 [thread overview]
Message-ID: <slrnbjpmd7.nq.lutz@taranis.iks-jena.de> (raw)
In-Reply-To: 3922594d.0308150346.65657e94@posting.google.com
* Alex Crivat wrote:
> Is there a way of doing this in Ada using operators or should I use a
> custom function;
You should use a custom function, but if you really need this, there is a
trick. The '()' syntax is used in two contexts. First as an array
subscript, as you like to recognise it. And second as a function call.
Using access variables to function calls the same syntax applies.
So you have to set a variable either to an array or a function. Because a
variable can't be of type function, use access to function instead. You only
need a slight modification to set the initial value.
I'll provide you the short and the generic solution:
------------------------------------------------------------------------
with Ada.Text_IO;
procedure t is
function Get(i : Natural) return Natural is
begin
return i + 4;
end Get;
type List is access function (i : Natural) return Natural;
a : Natural;
f : List := Get'Access;
begin
a := f(3);
Ada.Text_IO.Put_Line("a =" & Natural'Image(a));
end t;
------------------------------------------------------------------------
with Ada.Text_IO;
procedure t is
generic
type Item (<>) is limited private;
type Offset (<>) is limited private;
with function Get(i : Offset) return Item is <>;
package GenList is
type List is access function (i : Offset) return Item;
Current : constant List;
private
function MyGet (i : Offset) return Item renames Get;
Current : constant List := MyGet'Access;
end GenList;
function Get(i : Natural) return Natural is
begin
return i + 4;
end Get;
package MyList is new GenList(Natural, Natural);
use MyList;
a : Natural;
f : List := Current;
begin
a := f(3);
Ada.Text_IO.Put_Line("a =" & Natural'Image(a));
end t;
------------------------------------------------------------------------
Have fun.
next prev parent reply other threads:[~2003-08-15 13:01 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-08-15 11:46 Indexed list Alex Crivat
2003-08-15 13:01 ` Lutz Donnerhacke [this message]
2003-08-15 23:57 ` Matthew Heaney
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox