comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Universal type in Ada
Date: Thu, 16 Jun 2005 16:31:14 +0200
Date: 2005-06-16T16:31:14+02:00	[thread overview]
Message-ID: <b5g504h13gmq$.1gphqvpvdhor4$.dlg@40tude.net> (raw)
In-Reply-To: 1118928231.090737.170070@g44g2000cwa.googlegroups.com

On 16 Jun 2005 06:23:51 -0700, zw@cs.man.ac.uk wrote:

> Hi, thank you very much. This is a great idea to create pointers to an
> Object.

No, you don't need pointers. Create can return Object'Class which is much
safer and easier to use.

The example I gave, was to illustrate a language problem. In a real case
the code looks different:

function Create_My_Object (...) return Object_Handle is
   Result  : Object_Ptr;
begin
   Result := new My_Object (...);
   declare
      This : My_Object renames  M_Object (Result.all);
   begin
      ...
   end;
   return Ref (Result); -- The result is a handle (for GC)
exception
   when others =>
       Free (Result); -- Don't want it leaking
end Create_My_Object;

Note the result is a handle instead of a raw pointer. This is closer to
Java spirit, but not necessarily a universal solution. Not everything needs
smart pointers. I wouldn't program a hard real-time application this way...
(:-))

> I also have the same idea, but my problem is a pointer must be
> declared with a specific type such as a pointer to an Integer. However,
> in Ada, when we declared Object as an abstract superclass, the existing
> types like Integer, Float, String are not an Object, we could easily
> define a superclass called "Object" and define all of our types
> inherited from the "Object", this way we will get roughly same
> functionalities like Object as the root type. However, there is no way
> to cope with existing types like Integer.

Yes. You have to aggregate:

type Integer_Object is new Object with record
   Value : Integer;
end record;

But, what are you looking for? Often people with a baggage of other
languages are asking for a language-X solution in Ada. It is not the best
way...

> I would like to know that
> whether this is possible to define a root type like "Object" in Java so
> that whatever a "Method" is, it will always return an "Object"? because
> all classes are inherited from "Object", so an Integer is an Object, a
> Float is an Object, ...........

Technically, this requires supertyping, which neither Ada nor Jave have.
Though normally types like Integer, Float, Boolean etc need not to be
"objects" in the sense that they all would have "Method" as a primitive
operation. Also "objects" are usually thought as something large having
identity. Well, this is wrong in general, but often right in particular.
(:-))

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  parent reply	other threads:[~2005-06-16 14:31 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-15 13:07 Universal type in Ada zw
2005-06-15 13:32 ` Larry Kilgallen
2005-06-16 13:13   ` zw
2005-06-16 14:36     ` Preben Randhol
2005-06-17  2:54     ` Stephen Leake
2005-06-19 17:38     ` Martin Krischik
2005-06-15 13:55 ` Dmitry A. Kazakov
2005-06-15 14:37   ` Preben Randhol
2005-06-15 16:48     ` Dmitry A. Kazakov
2005-06-15 14:34 ` Preben Randhol
2005-06-16 13:10   ` zw
2005-06-15 16:31 ` Georg Bauhaus
2005-06-16  8:18 ` Ole-Hjalmar Kristensen
2005-06-16 11:59   ` Dmitry A. Kazakov
2005-06-16 13:23     ` zw
2005-06-16 14:19       ` jimmaureenrogers
2005-06-16 14:31       ` Dmitry A. Kazakov [this message]
2005-06-19 17:31       ` Martin Krischik
2005-06-19 17:26 ` Martin Krischik
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox