* Universal type in Ada @ 2005-06-15 13:07 zw 2005-06-15 13:32 ` Larry Kilgallen ` (5 more replies) 0 siblings, 6 replies; 19+ messages in thread From: zw @ 2005-06-15 13:07 UTC (permalink / raw) Hi, I am trying to create a Universal type in Ada such as "Object" in Java so that I could define a function that returns a value in this Universal type, then I could lower-cast value in this type to its specific type such as String, Float or any user-defined types. Because in Java if a method returns an Object, it could be casted to any other types that are subtypes of Object. Is there a Universal type in Ada? Could anyone tell me how to create such types in Ada, please? ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 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-15 13:55 ` Dmitry A. Kazakov ` (4 subsequent siblings) 5 siblings, 1 reply; 19+ messages in thread From: Larry Kilgallen @ 2005-06-15 13:32 UTC (permalink / raw) In article <bf72da73.0506150507.6058c851@posting.google.com>, zw@cs.man.ac.uk (zw) writes: > Hi, I am trying to create a Universal type in Ada such as "Object" in > Java so that I could define a function that returns a value in this > Universal type, then I could lower-cast value in this type to its > specific type such as String, Float or any user-defined types. Because > in Java if a method returns an Object, it could be casted to any other > types that are subtypes of Object. Is there a Universal type in Ada? > Could anyone tell me how to create such types in Ada, please? That is not a very Ada-like way to program. Are absolutely certain you are not trying to transfer programming habits from other languages over to Ada ? ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 2005-06-15 13:32 ` Larry Kilgallen @ 2005-06-16 13:13 ` zw 2005-06-16 14:36 ` Preben Randhol ` (2 more replies) 0 siblings, 3 replies; 19+ messages in thread From: zw @ 2005-06-16 13:13 UTC (permalink / raw) Yes, I am trying to transfer a program in Java to Ada. Because as far as I know there is no reflection mechanisms in Ada, so what I do is to try to create a new API for reflection in Ada that would do someting similar to java.lang.reflect ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 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 2 siblings, 0 replies; 19+ messages in thread From: Preben Randhol @ 2005-06-16 14:36 UTC (permalink / raw) To: comp.lang.ada Organization: PVV zw@cs.man.ac.uk wrote on 16/06/2005 (13:17) : > Yes, I am trying to transfer a program in Java to Ada. Because as far > as I know there is no reflection mechanisms in Ada, so what I do is to > try to create a new API for reflection in Ada that would do someting > similar to java.lang.reflect If you are transfering, why not improve the program at the same time by using Ada's powers? Doesn't make sense to me to transfer the program unless there is a benifit from it. But I don't know the program, so there may be other reasons that one need to cheat the type system. Preben ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 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 2 siblings, 0 replies; 19+ messages in thread From: Stephen Leake @ 2005-06-17 2:54 UTC (permalink / raw) To: zw; +Cc: comp.lang.ada zw@cs.man.ac.uk writes: > Yes, I am trying to transfer a program in Java to Ada. Because as far > as I know there is no reflection mechanisms in Ada, Reflection in Ada is provided by ASIS. It's a separate pass at compile time, rather than integrated at run time. So it is a different flavor. > so what I do is to try to create a new API for reflection in Ada > that would do someting similar to java.lang.reflect Depending on what you want to do with reflection, ASIS might be the right answer. -- -- Stephe ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 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 2 siblings, 0 replies; 19+ messages in thread From: Martin Krischik @ 2005-06-19 17:38 UTC (permalink / raw) zw@cs.man.ac.uk wrote: > Yes, I am trying to transfer a program in Java to Ada. Because as far > as I know there is no reflection mechanisms in Ada, so what I do is to > try to create a new API for reflection in Ada that would do someting > similar to java.lang.reflect Not quite true. Ada does have some form of reflection via the tag. You could use the external tag or the expanded name to determine the type of an object. http://www.adaic.com/standards/rm-amend/html/RM-3-9.html Also the operator "in" can be helpfull. And just like Java Ada will issue an exception when you convert objects which should not be converted. Martin -- mailto://krischik@users.sourceforge.net Ada programming at: http://ada.krischik.com ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 2005-06-15 13:07 Universal type in Ada zw 2005-06-15 13:32 ` Larry Kilgallen @ 2005-06-15 13:55 ` Dmitry A. Kazakov 2005-06-15 14:37 ` Preben Randhol 2005-06-15 14:34 ` Preben Randhol ` (3 subsequent siblings) 5 siblings, 1 reply; 19+ messages in thread From: Dmitry A. Kazakov @ 2005-06-15 13:55 UTC (permalink / raw) On 15 Jun 2005 06:07:12 -0700, zw wrote: > Hi, I am trying to create a Universal type in Ada such as "Object" in > Java so that I could define a function that returns a value in this > Universal type, then I could lower-cast value in this type to its > specific type such as String, Float or any user-defined types. Because > in Java if a method returns an Object, it could be casted to any other > types that are subtypes of Object. Is there a Universal type in Ada? Yes. What you describe is a contravariant result. In Ada it is achieved by returning a class-wide result. A function of this sort is also called "abstract factory". > Could anyone tell me how to create such types in Ada, please? type Object is abstract tagged null record; function Factory (...) return Object'Class; type My_Special_Object is new Object with ...; declare X : My_Special_Object; begin ... X := My_Special_Object Factory (...)); Here the result of Factory is explicitly converted to My_Special_Object. If that is impossible (because the result is not a My_Special_Object) Constraint_Error exception is propagated at run-time. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 2005-06-15 13:55 ` Dmitry A. Kazakov @ 2005-06-15 14:37 ` Preben Randhol 2005-06-15 16:48 ` Dmitry A. Kazakov 0 siblings, 1 reply; 19+ messages in thread From: Preben Randhol @ 2005-06-15 14:37 UTC (permalink / raw) To: comp.lang.ada "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote on 15/06/2005 (14:00) : > > type Object is abstract tagged null record; > function Factory (...) return Object'Class; > > type My_Special_Object is new Object with ...; > > declare > X : My_Special_Object; > begin > ... > X := My_Special_Object Factory (...)); X := My_Special_Object (Factory (...)); right? Preben ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 2005-06-15 14:37 ` Preben Randhol @ 2005-06-15 16:48 ` Dmitry A. Kazakov 0 siblings, 0 replies; 19+ messages in thread From: Dmitry A. Kazakov @ 2005-06-15 16:48 UTC (permalink / raw) On Wed, 15 Jun 2005 16:37:46 +0200, Preben Randhol wrote: > "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote on 15/06/2005 (14:00) : >> >> type Object is abstract tagged null record; >> function Factory (...) return Object'Class; >> >> type My_Special_Object is new Object with ...; >> >> declare >> X : My_Special_Object; >> begin >> ... >> X := My_Special_Object Factory (...)); > > X := My_Special_Object (Factory (...)); > > right? Yes, of course. (:-)) -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 2005-06-15 13:07 Universal type in Ada zw 2005-06-15 13:32 ` Larry Kilgallen 2005-06-15 13:55 ` Dmitry A. Kazakov @ 2005-06-15 14:34 ` Preben Randhol 2005-06-16 13:10 ` zw 2005-06-15 16:31 ` Georg Bauhaus ` (2 subsequent siblings) 5 siblings, 1 reply; 19+ messages in thread From: Preben Randhol @ 2005-06-15 14:34 UTC (permalink / raw) To: comp.lang.ada zw <zw@cs.man.ac.uk> wrote on 15/06/2005 (13:11) : > Hi, I am trying to create a Universal type in Ada such as "Object" in > Java so that I could define a function that returns a value in this > Universal type, then I could lower-cast value in this type to its > specific type such as String, Float or any user-defined types. Because > in Java if a method returns an Object, it could be casted to any other > types that are subtypes of Object. Is there a Universal type in Ada? > Could anyone tell me how to create such types in Ada, please? Why would you want to do something like this? Generally you don't want to use subtypes over types as you loose the advantage of the Ada type checking... Remember that in Ada one can have: function Method (Some_Input : Input_Type) return Integer; function Method (Some_Input : Input_Type) return Float; function Method (Some_Input : Input_Type) return String; And do: Number : Integer := Method (Input); Height : Float := Method (Input); Text : String := Method (Input); then the correct function will be called depending on return type. Please explain what you want to do and we can give better hints to do it in more Ada-wise ways. Preben ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 2005-06-15 14:34 ` Preben Randhol @ 2005-06-16 13:10 ` zw 0 siblings, 0 replies; 19+ messages in thread From: zw @ 2005-06-16 13:10 UTC (permalink / raw) Preben Randhol wrote: > zw <zw@cs.man.ac.uk> wrote on 15/06/2005 (13:11) : > > Hi, I am trying to create a Universal type in Ada such as "Object" in > > Java so that I could define a function that returns a value in this > > Universal type, then I could lower-cast value in this type to its > > specific type such as String, Float or any user-defined types. Because > > in Java if a method returns an Object, it could be casted to any other > > types that are subtypes of Object. Is there a Universal type in Ada? > > Could anyone tell me how to create such types in Ada, please? > > Why would you want to do something like this? Generally you don't want > to use subtypes over types as you loose the advantage of the Ada type > checking... > > Remember that in Ada one can have: > > function Method (Some_Input : Input_Type) return Integer; > function Method (Some_Input : Input_Type) return Float; > function Method (Some_Input : Input_Type) return String; > > And do: > > Number : Integer := Method (Input); > Height : Float := Method (Input); > Text : String := Method (Input); > > then the correct function will be called depending on return type. > > Please explain what you want to do and we can give better hints to do it > in more Ada-wise ways. > > Preben Hi, Thank you very much. We could define a function "Method" that takes same inputs and return different types of results. However, we must know the type of result from a "Method" so that we could declare "Result" as an Integer, Float or String, but the problem is when we don't know the "Result" type, we could not use the function "Method". For example, in Java, .......... Object Method(Input_Type Some_Input){....} .......... Input_Type S1; Object result = Method(S1); .......... where result could an Integer, a Float or an instance of a user-defined Object. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 2005-06-15 13:07 Universal type in Ada zw ` (2 preceding siblings ...) 2005-06-15 14:34 ` Preben Randhol @ 2005-06-15 16:31 ` Georg Bauhaus 2005-06-16 8:18 ` Ole-Hjalmar Kristensen 2005-06-19 17:26 ` Martin Krischik 5 siblings, 0 replies; 19+ messages in thread From: Georg Bauhaus @ 2005-06-15 16:31 UTC (permalink / raw) zw wrote: > Because > in Java if a method returns an Object, it could be casted to any other > types that are subtypes of Object. Depends on what you mean by "could". You will have to know the type of your object unless a ClassCastException is o.K.. You could start expressing a minimal set of expectations that your Ada program is going to have of a type. Then use polymorphism down that type's hierarchy. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 2005-06-15 13:07 Universal type in Ada zw ` (3 preceding siblings ...) 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-19 17:26 ` Martin Krischik 5 siblings, 1 reply; 19+ messages in thread From: Ole-Hjalmar Kristensen @ 2005-06-16 8:18 UTC (permalink / raw) Please do not do this. As someone (may have been John Max Skaller) on comp.lang.c++ said once, "casting is something you do when fishing". Think about what you are trying to do, and try to figure out how this can be accomplished by the tools available to you in Ada, like generics or run-time polymorphism. >>>>> "z" == zw <zw@cs.man.ac.uk> writes: z> Hi, I am trying to create a Universal type in Ada such as "Object" in z> Java so that I could define a function that returns a value in this z> Universal type, then I could lower-cast value in this type to its z> specific type such as String, Float or any user-defined types. Because z> in Java if a method returns an Object, it could be casted to any other z> types that are subtypes of Object. Is there a Universal type in Ada? z> Could anyone tell me how to create such types in Ada, please? -- The Sun also rises ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 2005-06-16 8:18 ` Ole-Hjalmar Kristensen @ 2005-06-16 11:59 ` Dmitry A. Kazakov 2005-06-16 13:23 ` zw 0 siblings, 1 reply; 19+ messages in thread From: Dmitry A. Kazakov @ 2005-06-16 11:59 UTC (permalink / raw) On 16 Jun 2005 10:18:44 +0200, Ole-Hjalmar Kristensen wrote: > Please do not do this. As someone (may have been John Max Skaller) on > comp.lang.c++ said once, "casting is something you do when fishing". > Think about what you are trying to do, and try to figure out how this > can be accomplished by the tools available to you in Ada, like > generics or run-time polymorphism. Yes, but unfortunately, there is one case in Ada 95 where apparently unnecessary downcasting is necessary: pool-specific access types to class-wide objects: type Object is tagged ...; type Object_Ptr is access Object'Class; function Create_My_Object (...) return Object_Ptr is Result : Object_Ptr; begin Result := new My_Object (...); declare This : My_Object renames M_Object (Result.all); -- Casting is required because a pool-specific access -- to My_Object is not convertible to a pool-specific -- access to Object'Class! begin -- Do whatever needed to complete initialization of -- This ... end; return Result; end Create_My_Object; -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 2005-06-16 11:59 ` Dmitry A. Kazakov @ 2005-06-16 13:23 ` zw 2005-06-16 14:19 ` jimmaureenrogers ` (2 more replies) 0 siblings, 3 replies; 19+ messages in thread From: zw @ 2005-06-16 13:23 UTC (permalink / raw) Hi, thank you very much. This is a great idea to create pointers to an Object. 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. 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, ........... ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 2005-06-16 13:23 ` zw @ 2005-06-16 14:19 ` jimmaureenrogers 2005-06-16 14:31 ` Dmitry A. Kazakov 2005-06-19 17:31 ` Martin Krischik 2 siblings, 0 replies; 19+ messages in thread From: jimmaureenrogers @ 2005-06-16 14:19 UTC (permalink / raw) zw@cs.man.ac.uk wrote: > Hi, thank you very much. This is a great idea to create pointers to an > Object. 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. 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, ........... Java does not solve this problem any better than Ada. The Java primitive types do not inherit from Object. Java provides wrapper classes for primitive types, but they cannot be used interchageably with the primitive types themselves, there is always a type conversion required. Ada allows you to create a tagged type, either concrete or abstract, as the root of an inheritance hierarchy. You can then deal with types such as T'Class and "access T'Class". Java requires reflection because it provides the Object class. One result of using a universal ancestor class is the confusion of having a reference possibly point to either a Float (the wrapper class for the float primitive type) or a Button (representing a GUI component). You must then have reflection capabilities so that you can determine what kind of Object you are pointing to. The methods inherited from Object are useful in a general sense, but not much help when trying to deal with characteristics of different classes that do not have anything in common except the Object class. Many Java designs include sloppy inheritance hierarchies encouraged by the availability of the Object references. If you are converting a program from Java to Ada you should try to do more than simply recreate a Java inheritance hierarchy using Ada syntax. You should re-design the program to use Ada as Ada. Consider converting an Ada program to Java. What if the Ada program used tasks with entries and the select clause in at least one of the tasks included a terminate option? How would you express that in Java syntax? Java has no direct equivalent to the Ada select clause. Java has no direct equivalent to the terminate option. Even more difficult, what if one task called the entry in another task using a selective entry call? Java provides no direct equivalent for a selective entry call. You would need to re-design the Ada program so that you could get equivalent functionality using Java syntax. You might not be able to implement some Ada features directly in Java. You might need to settle for similar, if not fully equivalent functionality. Jim Rogers ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 2005-06-16 13:23 ` zw 2005-06-16 14:19 ` jimmaureenrogers @ 2005-06-16 14:31 ` Dmitry A. Kazakov 2005-06-19 17:31 ` Martin Krischik 2 siblings, 0 replies; 19+ messages in thread From: Dmitry A. Kazakov @ 2005-06-16 14:31 UTC (permalink / raw) 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 ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 2005-06-16 13:23 ` zw 2005-06-16 14:19 ` jimmaureenrogers 2005-06-16 14:31 ` Dmitry A. Kazakov @ 2005-06-19 17:31 ` Martin Krischik 2 siblings, 0 replies; 19+ messages in thread From: Martin Krischik @ 2005-06-19 17:31 UTC (permalink / raw) zw@cs.man.ac.uk wrote: > his is a great idea to create pointers to an > Object. Well, your don't use pointers to objects in Ada - you usually use class wide types - and *sometimes* pointers to class wide types. http://en.wikibooks.org/wiki/Programming:Ada:OO#The_class-wide_type Martin -- mailto://krischik@users.sourceforge.net Ada programming at: http://ada.krischik.com ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Universal type in Ada 2005-06-15 13:07 Universal type in Ada zw ` (4 preceding siblings ...) 2005-06-16 8:18 ` Ole-Hjalmar Kristensen @ 2005-06-19 17:26 ` Martin Krischik 5 siblings, 0 replies; 19+ messages in thread From: Martin Krischik @ 2005-06-19 17:26 UTC (permalink / raw) zw wrote: > Hi, I am trying to create a Universal type in Ada such as "Object" in > Java so that I could define a function that returns a value in this > Universal type, then I could lower-cast value in this type to its > specific type such as String, Float or any user-defined types. Because > in Java if a method returns an Object, it could be casted to any other > types that are subtypes of Object. In Ada you don't usually "cast" types you convert them. Read the article about type conversions: http://en.wikibooks.org/wiki/Programming:Ada:Subtypes#Converting_Data > Is there a Universal type in Ada? > Could anyone tell me how to create such types in Ada, please? A tagged type will do: http://en.wikibooks.org/wiki/Programming:Ada:OO Martin -- mailto://krischik@users.sourceforge.net Ada programming at: http://ada.krischik.com ^ permalink raw reply [flat|nested] 19+ messages in thread
end of thread, other threads:[~2005-06-19 17:38 UTC | newest] Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 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 2005-06-19 17:31 ` Martin Krischik 2005-06-19 17:26 ` Martin Krischik
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox