* type access Parent'Class @ 2011-11-21 19:03 Yukicanis 2011-11-21 19:25 ` Adam Beneschan ` (2 more replies) 0 siblings, 3 replies; 15+ messages in thread From: Yukicanis @ 2011-11-21 19:03 UTC (permalink / raw) Dear Group, I'm new to Ada and currently playing around with its OOP features. Now, I have the following source: procedure Test is type Parent is tagged limited null record; type Parent_Access is access Parent'Class; A : access Parent'Class; B : Parent_Access; procedure Dyn_Disp(X : access Parent'Class) is begin A := X; B := X; end Dyn_Disp; begin null; end Test; When I try to complie that with GNAT 4.6.1 I get the following error message: test.adb:9:10: expected type "Parent_Access" defined at line 3 test.adb:9:10: found type access to "Parent'Class" defined at line 6 which I don't really understand since type "Parent_Access" is type access to Parent'Class, isn't it? Thanks in advance. ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: type access Parent'Class 2011-11-21 19:03 type access Parent'Class Yukicanis @ 2011-11-21 19:25 ` Adam Beneschan 2011-11-21 19:40 ` Yukicanis 2011-11-21 23:45 ` Gene 2011-11-21 19:33 ` Robert A Duff 2011-11-21 21:09 ` Jeffrey Carter 2 siblings, 2 replies; 15+ messages in thread From: Adam Beneschan @ 2011-11-21 19:25 UTC (permalink / raw) On Nov 21, 11:03 am, Yukicanis <yukica...@googlemail.com> wrote: > Dear Group, > > I'm new to Ada and currently playing around with its OOP features. > Now, I have the following source: > > procedure Test is > type Parent is tagged limited null record; > type Parent_Access is access Parent'Class; > A : access Parent'Class; > B : Parent_Access; > procedure Dyn_Disp(X : access Parent'Class) is > begin > A := X; > B := X; > end Dyn_Disp; > begin > null; > end Test; > > When I try to complie that with GNAT 4.6.1 I get the following error > message: > > test.adb:9:10: expected type "Parent_Access" defined at line 3 > test.adb:9:10: found type access to "Parent'Class" defined at line 6 > > which I don't really understand since type "Parent_Access" is type > access to Parent'Class, isn't it? > > Thanks in advance. Ada is a strongly typed language. That means that you can have two types that look exactly the same but are still different types, because you've declared them as different types, and you can't just use them interchangeably. Example: type Acc_Type_1 is access all Integer; type Acc_Type_2 is access all Integer; V1 : Acc_Type_1; V2 : Acc_Type_2; V1 := V2; -- ILLEGAL! This is illegal because V1 and V2 have different types, even though the types look the same. You have to use a type conversion: V1 := Acc_Type_1(V2); -- LEGAL This doesn't have anything to do with access types, by the way. All types in Ada are like that. In your original example, "access Parent'Class" is an anonymous type, and every time an anonymous type occurs, it's a new type. So that's why B := X; is illegal. You have to use a type conversion: B := Parent_Access(X); That still won't quite work, because anonymous access types are "general" access types and their values can point to any aliased Parent'Class object. However, Parent_Access is called a "pool- specific" access type because it can only point to objects that are allocated with "new". To make Parent_Access a general access type, add an "all": type Parent_Access is access all Parent'Class; and now your example will compile. In my view, there isn't much reason *not* to use "all" in an access type declaration, and I've gotten in the habit of including it routinely. (But you can't use "all" like this if it's not in a TYPE declaration.) The language allows A := X; even though A and X have different types, because there's no way to write a type conversion. This is an exception to the strong-typing rules. Hope this helps, -- Adam ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: type access Parent'Class 2011-11-21 19:25 ` Adam Beneschan @ 2011-11-21 19:40 ` Yukicanis 2011-11-21 19:45 ` Robert A Duff 2011-11-21 23:45 ` Gene 1 sibling, 1 reply; 15+ messages in thread From: Yukicanis @ 2011-11-21 19:40 UTC (permalink / raw) On Nov 21, 8:25 pm, Adam Beneschan <a...@irvine.com> wrote: > On Nov 21, 11:03 am, Yukicanis <yukica...@googlemail.com> wrote: > > > > > > > > > > > Dear Group, > > > I'm new to Ada and currently playing around with its OOP features. > > Now, I have the following source: > > > procedure Test is > > type Parent is tagged limited null record; > > type Parent_Access is access Parent'Class; > > A : access Parent'Class; > > B : Parent_Access; > > procedure Dyn_Disp(X : access Parent'Class) is > > begin > > A := X; > > B := X; > > end Dyn_Disp; > > begin > > null; > > end Test; > > > When I try to complie that with GNAT 4.6.1 I get the following error > > message: > > > test.adb:9:10: expected type "Parent_Access" defined at line 3 > > test.adb:9:10: found type access to "Parent'Class" defined at line 6 > > > which I don't really understand since type "Parent_Access" is type > > access to Parent'Class, isn't it? > > > Thanks in advance. > > Ada is a strongly typed language. That means that you can have two > types that look exactly the same but are still different types, > because you've declared them as different types, and you can't just > use them interchangeably. Example: > > type Acc_Type_1 is access all Integer; > type Acc_Type_2 is access all Integer; > V1 : Acc_Type_1; > V2 : Acc_Type_2; > > V1 := V2; -- ILLEGAL! > > This is illegal because V1 and V2 have different types, even though > the types look the same. You have to use a type conversion: > > V1 := Acc_Type_1(V2); -- LEGAL > > This doesn't have anything to do with access types, by the way. All > types in Ada are like that. > > In your original example, "access Parent'Class" is an anonymous type, > and every time an anonymous type occurs, it's a new type. So that's > why > > B := X; > > is illegal. You have to use a type conversion: > > B := Parent_Access(X); > > That still won't quite work, because anonymous access types are > "general" access types and their values can point to any aliased > Parent'Class object. However, Parent_Access is called a "pool- > specific" access type because it can only point to objects that are > allocated with "new". To make Parent_Access a general access type, > add an "all": > > type Parent_Access is access all Parent'Class; > > and now your example will compile. In my view, there isn't much > reason *not* to use "all" in an access type declaration, and I've > gotten in the habit of including it routinely. (But you can't use > "all" like this if it's not in a TYPE declaration.) > > The language allows > > A := X; > > even though A and X have different types, because there's no way to > write a type conversion. This is an exception to the strong-typing > rules. > > Hope this helps, > > -- Adam Yes, that helps me a lot! That you very much! BTW, I just discovered that B := A.all'Access; also seems to work (with access all). But won't the rigth hand side result in an anonymous type again? Thanks in advance. ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: type access Parent'Class 2011-11-21 19:40 ` Yukicanis @ 2011-11-21 19:45 ` Robert A Duff 2011-11-21 19:46 ` Yukicanis 0 siblings, 1 reply; 15+ messages in thread From: Robert A Duff @ 2011-11-21 19:45 UTC (permalink / raw) Yukicanis <yukicanis@googlemail.com> writes: > BTW, I just discovered that > > B := A.all'Access; > > also seems to work (with access all). But won't the rigth hand side > result in an anonymous type again? No, the type of Blah'Access is always taken from the context (the expected type, which is the (named) type of B in this case). - Bob ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: type access Parent'Class 2011-11-21 19:45 ` Robert A Duff @ 2011-11-21 19:46 ` Yukicanis 0 siblings, 0 replies; 15+ messages in thread From: Yukicanis @ 2011-11-21 19:46 UTC (permalink / raw) On Nov 21, 8:45 pm, Robert A Duff <bobd...@shell01.TheWorld.com> wrote: > Yukicanis <yukica...@googlemail.com> writes: > > BTW, I just discovered that > > > B := A.all'Access; > > > also seems to work (with access all). But won't the rigth hand side > > result in an anonymous type again? > > No, the type of Blah'Access is always taken from the context > (the expected type, which is the (named) type of B in this case). > > - Bob Thank you very much! Everything seems to be clean now. :) ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: type access Parent'Class 2011-11-21 19:25 ` Adam Beneschan 2011-11-21 19:40 ` Yukicanis @ 2011-11-21 23:45 ` Gene 2011-11-22 8:42 ` Dmitry A. Kazakov 1 sibling, 1 reply; 15+ messages in thread From: Gene @ 2011-11-21 23:45 UTC (permalink / raw) On Nov 21, 8:25 pm, Adam Beneschan <a...@irvine.com> wrote: > On Nov 21, 11:03 am, Yukicanis <yukica...@googlemail.com> wrote: > > > > > > > Dear Group, > > > I'm new to Ada and currently playing around with its OOP features. > > Now, I have the following source: > > > procedure Test is > > type Parent is tagged limited null record; > > type Parent_Access is access Parent'Class; > > A : access Parent'Class; > > B : Parent_Access; > > procedure Dyn_Disp(X : access Parent'Class) is > > begin > > A := X; > > B := X; > > end Dyn_Disp; > > begin > > null; > > end Test; > > > When I try to complie that with GNAT 4.6.1 I get the following error > > message: > > > test.adb:9:10: expected type "Parent_Access" defined at line 3 > > test.adb:9:10: found type access to "Parent'Class" defined at line 6 > > > which I don't really understand since type "Parent_Access" is type > > access to Parent'Class, isn't it? > > > Thanks in advance. > > Ada is a strongly typed language. That means that you can have two > types that look exactly the same but are still different types, > because you've declared them as different types, and you can't just > use them interchangeably. Example: Strong typing doesn't isn't quite the right explanation. Rather I think you want to say that Ada type equality and compatibility are defined in terms of static type names. There is also the rule that anonymous types are inequivalent to any other type. However, there exist languages that are strongly typed that don't rely (at least entirely) on name equivalence. Rather they use some variation of structural equivalence. ML is in this category. ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: type access Parent'Class 2011-11-21 23:45 ` Gene @ 2011-11-22 8:42 ` Dmitry A. Kazakov 2011-11-22 10:07 ` Georg Bauhaus 0 siblings, 1 reply; 15+ messages in thread From: Dmitry A. Kazakov @ 2011-11-22 8:42 UTC (permalink / raw) On Mon, 21 Nov 2011 15:45:24 -0800 (PST), Gene wrote: > However, there exist languages that are strongly typed that don't rely > (at least entirely) on name equivalence. Rather they use some > variation of structural equivalence. ML is in this category. Now, if the type behavior is indeterminable from the type's structure, then structured equivalence imply weak typing = the behavior varies with the interpretation. Otherwise (the behavior is exhaustively determined by type's structure) the type system is weak. I don't really know ML, so I can only suggest that it falls into the first category. P.S. Strong typing became a kind of fashion. In these days almost any language claims to be strongly typed. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: type access Parent'Class 2011-11-22 8:42 ` Dmitry A. Kazakov @ 2011-11-22 10:07 ` Georg Bauhaus 2011-11-22 13:27 ` Simon Wright 0 siblings, 1 reply; 15+ messages in thread From: Georg Bauhaus @ 2011-11-22 10:07 UTC (permalink / raw) On 22.11.11 09:42, Dmitry A. Kazakov wrote: > On Mon, 21 Nov 2011 15:45:24 -0800 (PST), Gene wrote: > >> However, there exist languages that are strongly typed that don't rely >> (at least entirely) on name equivalence. Rather they use some >> variation of structural equivalence. ML is in this category. > > Now, if the type behavior is indeterminable from the type's structure, then > structured equivalence imply weak typing = the behavior varies with the > interpretation. Otherwise (the behavior is exhaustively determined by > type's structure) the type system is weak. I don't really know ML, so I can > only suggest that it falls into the first category. > > P.S. Strong typing became a kind of fashion. In these days almost any > language claims to be strongly typed. Nice to see people discuss variations of type systems; the following quote is from notes on reading Haskell programs, for Python programmers, http://blog.ezyang.com/2011/11/how-to-read-haskell/ . "Types. Ignore everything you see after :: (similarly, you can ignore type, class, instance and newtype. Some people claim that types help them understand code; if you're a complete beginner, things like Int and String will probably help, and things like LayoutClass and MonadError won't. Don't worry too much about it.)" I am not sure whether or not this is good advice, since indeed beginners might see types as yet another hurdle that gets in the way of understanding how things work. But this advice addresses Python programmers, not people new to programming. Right now I am seeing, again, what it means to rewrite portions of a program when there is no static type system (and no parameter naming). Think of subprograms that have defaulted parameters, now drop one parameter, permute their order, ... and no compiler (sometimes not even pylint) telling you that this *will* *cause* a type error at run time---as is the case with Python. __ Did the author (intentionally) miss the fact that monads, TTBOMK, add a formal framework for reasoning (somewhat) about I/O in purely functional programs? If not in terms of time and space, then surely in terms of data flowing into, flowing through, and flowing out of functions-whose-profile-includes-world? Ada's I/O related types kind of allows this, I'd think, to the extent that one can answer questions: "Which units call operations on types having to do with I/O"? ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: type access Parent'Class 2011-11-22 10:07 ` Georg Bauhaus @ 2011-11-22 13:27 ` Simon Wright 2011-11-22 16:13 ` Georg Bauhaus 0 siblings, 1 reply; 15+ messages in thread From: Simon Wright @ 2011-11-22 13:27 UTC (permalink / raw) Georg Bauhaus <rm.dash-bauhaus@futureapps.de> writes: > Right now I am seeing, again, what it means to rewrite portions of a > program when there is no static type system (and no parameter naming). I can see your point if you mean no parameter _typing_ ... ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: type access Parent'Class 2011-11-22 13:27 ` Simon Wright @ 2011-11-22 16:13 ` Georg Bauhaus 0 siblings, 0 replies; 15+ messages in thread From: Georg Bauhaus @ 2011-11-22 16:13 UTC (permalink / raw) On 22.11.11 14:27, Simon Wright wrote: > Georg Bauhaus <rm.dash-bauhaus@futureapps.de> writes: > >> Right now I am seeing, again, what it means to rewrite portions of a >> program when there is no static type system (and no parameter naming). > > I can see your point if you mean no parameter _typing_ ... Yes, though both static typing and naming of parameters will have an effect, insofar as the Python interpreter will report a type error in case I mistype a parameter name. Pylint will catch the error at "compile time": $ cat catch.py def newline(times=1): for run in range(times): print newline() newline(count=2) $ pylint --errors-only catch.py No config file found, using default configuration ************* Module catch E: 7: Passing unexpected keyword argument 'count' in function call $ python catch.py Traceback (most recent call last): File "catch.py", line 7, in <module> newline(count=2) TypeError: newline() got an unexpected keyword argument 'count' ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: type access Parent'Class 2011-11-21 19:03 type access Parent'Class Yukicanis 2011-11-21 19:25 ` Adam Beneschan @ 2011-11-21 19:33 ` Robert A Duff 2011-11-21 19:44 ` Yukicanis 2011-11-24 10:33 ` Yannick Duchêne (Hibou57) 2011-11-21 21:09 ` Jeffrey Carter 2 siblings, 2 replies; 15+ messages in thread From: Robert A Duff @ 2011-11-21 19:33 UTC (permalink / raw) Yukicanis <yukicanis@googlemail.com> writes: > Dear Group, > > I'm new to Ada and currently playing around with its OOP features. > Now, I have the following source: > > procedure Test is > type Parent is tagged limited null record; > type Parent_Access is access Parent'Class; > A : access Parent'Class; > B : Parent_Access; > procedure Dyn_Disp(X : access Parent'Class) is > begin > A := X; > B := X; > end Dyn_Disp; > begin > null; > end Test; > > When I try to complie that with GNAT 4.6.1 I get the following error > message: > > test.adb:9:10: expected type "Parent_Access" defined at line 3 > test.adb:9:10: found type access to "Parent'Class" defined at line 6 > > which I don't really understand since type "Parent_Access" is type > access to Parent'Class, isn't it? No, there are three different access types -- Parent_Access, the anonymous type of A, and the anonymous type of X. They're all "access to Parent'Class", but they are distinct. There are some implicit conversion rules that make "A := X;" legal. But there is no implicit conversion to Parent_Access, so "B := X;" is illegal. You normally want access-to-class-wide types (and many other access types) to be "general", which is specified by adding "all": type Parent_Access is access all Parent'Class; If you do that, then you could say "B := Parent_Access(X);" or "B := A.all'Access;". But I suggest you avoid anonymous access types, except in certain special cases. They are confusing. I also suggest you put most of your code in packages, rather than in the main procedure. That will also help avoid confusion, because things behave differently when inside procedures. For example, you can't have any dispatching procedures unless you put the type in a package spec. - Bob ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: type access Parent'Class 2011-11-21 19:33 ` Robert A Duff @ 2011-11-21 19:44 ` Yukicanis 2011-11-24 10:33 ` Yannick Duchêne (Hibou57) 1 sibling, 0 replies; 15+ messages in thread From: Yukicanis @ 2011-11-21 19:44 UTC (permalink / raw) On Nov 21, 8:33 pm, Robert A Duff <bobd...@shell01.TheWorld.com> wrote: > Yukicanis <yukica...@googlemail.com> writes: > > Dear Group, > > > I'm new to Ada and currently playing around with its OOP features. > > Now, I have the following source: > > > procedure Test is > > type Parent is tagged limited null record; > > type Parent_Access is access Parent'Class; > > A : access Parent'Class; > > B : Parent_Access; > > procedure Dyn_Disp(X : access Parent'Class) is > > begin > > A := X; > > B := X; > > end Dyn_Disp; > > begin > > null; > > end Test; > > > When I try to complie that with GNAT 4.6.1 I get the following error > > message: > > > test.adb:9:10: expected type "Parent_Access" defined at line 3 > > test.adb:9:10: found type access to "Parent'Class" defined at line 6 > > > which I don't really understand since type "Parent_Access" is type > > access to Parent'Class, isn't it? > > No, there are three different access types -- Parent_Access, > the anonymous type of A, and the anonymous type of X. They're all > "access to Parent'Class", but they are distinct. > > There are some implicit conversion rules that make "A := X;" legal. > But there is no implicit conversion to Parent_Access, so "B := X;" > is illegal. > > You normally want access-to-class-wide types (and many other access > types) to be "general", which is specified by adding "all": > > type Parent_Access is access all Parent'Class; > > If you do that, then you could say "B := Parent_Access(X);" > or "B := A.all'Access;". > > But I suggest you avoid anonymous access types, except in certain > special cases. They are confusing. > > I also suggest you put most of your code in packages, rather than > in the main procedure. That will also help avoid confusion, because > things behave differently when inside procedures. For example, > you can't have any dispatching procedures unless you put the type > in a package spec. > > - Bob Thanks for your replay. I know that things behave differnetly in packages. I just wanted the minimal example to be as short es possible to avoid spamming. ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: type access Parent'Class 2011-11-21 19:33 ` Robert A Duff 2011-11-21 19:44 ` Yukicanis @ 2011-11-24 10:33 ` Yannick Duchêne (Hibou57) 2011-11-24 11:18 ` Yukicanis 1 sibling, 1 reply; 15+ messages in thread From: Yannick Duchêne (Hibou57) @ 2011-11-24 10:33 UTC (permalink / raw) Le Mon, 21 Nov 2011 20:33:22 +0100, Robert A Duff <bobduff@shell01.theworld.com> a écrit: > I also suggest you put most of your code in packages, rather than > in the main procedure. That will also help avoid confusion, because > things behave differently when inside procedures. For example, > you can't have any dispatching procedures unless you put the type > in a package spec. And also because Access to stuffs at library level is often more handy and more general. -- “Syntactic sugar causes cancer of the semi-colons.” [1] “Structured Programming supports the law of the excluded muddle.” [1] [1]: [Epigrams on Programming — Alan J. — P. Yale University] ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: type access Parent'Class 2011-11-24 10:33 ` Yannick Duchêne (Hibou57) @ 2011-11-24 11:18 ` Yukicanis 0 siblings, 0 replies; 15+ messages in thread From: Yukicanis @ 2011-11-24 11:18 UTC (permalink / raw) On 24 Nov., 11:33, Yannick Duchêne (Hibou57) <yannick_duch...@yahoo.fr> wrote: > Le Mon, 21 Nov 2011 20:33:22 +0100, Robert A Duff > <bobd...@shell01.theworld.com> a écrit:> I also suggest you put most of your code in packages, rather than > > in the main procedure. That will also help avoid confusion, because > > things behave differently when inside procedures. For example, > > you can't have any dispatching procedures unless you put the type > > in a package spec. > > And also because Access to stuffs at library level is often more handy and > more general. > > -- > “Syntactic sugar causes cancer of the semi-colons.” [1] > “Structured Programming supports the law of the excluded muddle.” [1] > [1]: [Epigrams on Programming — Alan J. — P. Yale University] Indeed. When I use C++, I also put every class in its own file. So I do with Ada. Thanks to everyone. It's nice to have this group. :) ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: type access Parent'Class 2011-11-21 19:03 type access Parent'Class Yukicanis 2011-11-21 19:25 ` Adam Beneschan 2011-11-21 19:33 ` Robert A Duff @ 2011-11-21 21:09 ` Jeffrey Carter 2 siblings, 0 replies; 15+ messages in thread From: Jeffrey Carter @ 2011-11-21 21:09 UTC (permalink / raw) On 11/21/2011 12:03 PM, Yukicanis wrote: > > procedure Test is > type Parent is tagged limited null record; > type Parent_Access is access Parent'Class; > A : access Parent'Class; > B : Parent_Access; > procedure Dyn_Disp(X : access Parent'Class) is > begin > A := X; > B := X; > end Dyn_Disp; > begin > null; > end Test; > > When I try to complie that with GNAT 4.6.1 I get the following error > message: > > test.adb:9:10: expected type "Parent_Access" defined at line 3 > test.adb:9:10: found type access to "Parent'Class" defined at line 6 > > which I don't really understand since type "Parent_Access" is type > access to Parent'Class, isn't it? The message should really say "found anonymous type access to ...", which points to the solution: don't use anonymous types. -- Jeff Carter "My mind is aglow with whirling, transient nodes of thought, careening through a cosmic vapor of invention." Blazing Saddles 85 ^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2011-11-24 11:42 UTC | newest] Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2011-11-21 19:03 type access Parent'Class Yukicanis 2011-11-21 19:25 ` Adam Beneschan 2011-11-21 19:40 ` Yukicanis 2011-11-21 19:45 ` Robert A Duff 2011-11-21 19:46 ` Yukicanis 2011-11-21 23:45 ` Gene 2011-11-22 8:42 ` Dmitry A. Kazakov 2011-11-22 10:07 ` Georg Bauhaus 2011-11-22 13:27 ` Simon Wright 2011-11-22 16:13 ` Georg Bauhaus 2011-11-21 19:33 ` Robert A Duff 2011-11-21 19:44 ` Yukicanis 2011-11-24 10:33 ` Yannick Duchêne (Hibou57) 2011-11-24 11:18 ` Yukicanis 2011-11-21 21:09 ` Jeffrey Carter
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox