www.digitalmars.com         C & C++   DMDScript  

D - Inner Classes

reply "Rupert Millard" <rupertamillard hotmail.dot.com> writes:
Hi all!

Does D sport inner classes / structs in any form?

For some reason, I assumed it did and have got into some trouble! Either way
I have at least one bug to report. If it is intended to have some support
for these could someone explain what ways members of the outer class can be
accessed?

From,

Rupert
Aug 05 2003
parent reply "Walter" <walter digitalmars.com> writes:
"Rupert Millard" <rupertamillard hotmail.dot.com> wrote in message
news:bgnto9$qnv$2 digitaldaemon.com...
 Hi all!

 Does D sport inner classes / structs in any form?

 For some reason, I assumed it did and have got into some trouble! Either

 I have at least one bug to report. If it is intended to have some support
 for these could someone explain what ways members of the outer class can

 accessed?

D supports nested classes, where the scopes are nested. It does not support java-style 'inner' classes. If you've found a bug, please post it here!
Aug 05 2003
parent reply "Rupert Millard" <rupertamillard hotmail.dot.com> writes:
Walter,

I doubt this code should be allowed to compile as a struct is nested within
a class, however it does compile and it produces this output on my machine
when executed:

in main():
x: 69

in B.displayX():
x: 4202672

I believe this behaviour is a bug.

As I was not able to find any help in the manual, could you explain exactly
what kind of nesting is allowed and what it permits you to do? Specifically
what does nested scopes mean?

Also, I understand that there is some ambiguity in the term 'inner classes'
in JAVA. Could somebody clarify exactly what it means?

Please excuse my ignorance, I am an entirely self-taught programmer doing it
as a hobby.

Thanks,

From,

Rupert


"Walter" <walter digitalmars.com> wrote in message
news:bgorth$1pmc$2 digitaldaemon.com...
 "Rupert Millard" <rupertamillard hotmail.dot.com> wrote in message
 news:bgnto9$qnv$2 digitaldaemon.com...
 Hi all!

 Does D sport inner classes / structs in any form?

 For some reason, I assumed it did and have got into some trouble! Either

 I have at least one bug to report. If it is intended to have some


 for these could someone explain what ways members of the outer class can

 accessed?

D supports nested classes, where the scopes are nested. It does not

 java-style 'inner' classes. If you've found a bug, please post it here!

Aug 06 2003
parent reply Gennadi Pais <Gennadi_member pathlink.com> writes:
Hi Rupert,

for the Java-style inner class explanation you can read the next link:
http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
Hope you'll find that is brief and clear.

About the "bug" you arose I think that is not a bug at all. Pay attention to
explanation about nested function that described either on the page 100 of the D
spec in pdf pormat or go to the http://www.digitalmars.com/d/function.html

When you declare (and define) a nested class in another one you just declare the
scope of that class, not a variable or some kind of instance that is connected
to concrete instance ot outer class. So an inner class can't receive a value of
non-static member of outer one, have no access to its frame.

Hope I've succeeded to explain the "bug" behavior.
Thanks!

In article <bgqm0m$g2h$1 digitaldaemon.com>, Rupert Millard says...
Walter,

I doubt this code should be allowed to compile as a struct is nested within
a class, however it does compile and it produces this output on my machine
when executed:

in main():
x: 69

in B.displayX():
x: 4202672

I believe this behaviour is a bug.

As I was not able to find any help in the manual, could you explain exactly
what kind of nesting is allowed and what it permits you to do? Specifically
what does nested scopes mean?

Also, I understand that there is some ambiguity in the term 'inner classes'
in JAVA. Could somebody clarify exactly what it means?

Please excuse my ignorance, I am an entirely self-taught programmer doing it
as a hobby.

Thanks,

From,

Rupert


"Walter" <walter digitalmars.com> wrote in message
news:bgorth$1pmc$2 digitaldaemon.com...
 "Rupert Millard" <rupertamillard hotmail.dot.com> wrote in message
 news:bgnto9$qnv$2 digitaldaemon.com...
 Hi all!

 Does D sport inner classes / structs in any form?

 For some reason, I assumed it did and have got into some trouble! Either

 I have at least one bug to report. If it is intended to have some


 for these could someone explain what ways members of the outer class can

 accessed?

D supports nested classes, where the scopes are nested. It does not

 java-style 'inner' classes. If you've found a bug, please post it here!

begin 666 bug.d M+R\ 0E5'. T*+R\ ;F5S=&5D(&-L87-S97, 9&]N)W0 8V]R<F5C=&QY(&%C M8V5S<R!M96UB97)S(&]F(&]U=&5R(&-L87-S#0HO+R!#3U!-4$E,15(Z#0HO M+R!$:6=I=&%L($UA<G, 1"!#;VUP:6QE<B!"971A('8P+C8X#0H-"FEM<&]R M="!C+G-T9&EO.PT*#0IC;&%S<R!!('L-" EI;G0 >#L-" T*"71H:7,H:6YT M(' R*0T*"7L-" D)>#UX,CL-" E]#0H)#0H)<')I=F%T92!S=')U8W0 0B![ M#0H)"0T*"0EV;VED(&1I<W!L87E8(" I#0H)"7L-" D)"7!R:6YT9B B:6X M0BYD:7-P;&%Y6" I.EQN( T*"0D)(" (" (")X.B E=5QN(BP >"D[#0H) M"7T-" E]#0H-" EP<FEV871E('9O:60 9&ES<&QA>5 H*0T*"7L-" D)0B!B M.PT*"0D-" D)8BYD:7-P;&%Y6" I.PT*"7T-"GT-" T*:6YT(&UA:6X *&-H M87);75M=(&%R9W,I#0I[#0H)02!A(#T ;F5W($$H-CDI.PT*"0T*"7!R:6YT M9B B:6X ;6%I;B I.EQN( T*"2 (" (" B>#H )74 7&Y<;B(L(&$N>"D[ E#0H)#0H)82YD:7-P;&%Y6" I.PT*"0T*"7)E='5R;B P.PT*?0`` ` end

Aug 06 2003
next sibling parent reply "Rupert Millard" <rupertamillard hotmail.dot.com> writes:
Gennadi,

Thanks for the link.

I don't see the relevance of nested functions to the problem in question,
however the manual says "Nested functions have access to the variables and
other symbols defined by the lexically enclosing function." If one were to
infer anything about nested classes from this, wouldn't it be that a nested
class could access the members of the enclosing class?

If what you go on to say is the case then shouldn't the compiler generate an
error (as it would do if B were a class instead of a struct) rather than
compiling the code?

From,

Rupert

"Gennadi Pais" <Gennadi_member pathlink.com> wrote in message
news:bgqq03$jgj$1 digitaldaemon.com...
 Hi Rupert,

 for the Java-style inner class explanation you can read the next link:
 http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
 Hope you'll find that is brief and clear.

 About the "bug" you arose I think that is not a bug at all. Pay attention

 explanation about nested function that described either on the page 100 of

 spec in pdf pormat or go to the http://www.digitalmars.com/d/function.html

 When you declare (and define) a nested class in another one you just

 scope of that class, not a variable or some kind of instance that is

 to concrete instance ot outer class. So an inner class can't receive a

 non-static member of outer one, have no access to its frame.

 Hope I've succeeded to explain the "bug" behavior.
 Thanks!

 In article <bgqm0m$g2h$1 digitaldaemon.com>, Rupert Millard says...
Walter,

I doubt this code should be allowed to compile as a struct is nested


a class, however it does compile and it produces this output on my


when executed:

in main():
x: 69

in B.displayX():
x: 4202672

I believe this behaviour is a bug.

As I was not able to find any help in the manual, could you explain


what kind of nesting is allowed and what it permits you to do?


what does nested scopes mean?

Also, I understand that there is some ambiguity in the term 'inner


in JAVA. Could somebody clarify exactly what it means?

Please excuse my ignorance, I am an entirely self-taught programmer doing


as a hobby.

Thanks,

From,

Rupert


"Walter" <walter digitalmars.com> wrote in message
news:bgorth$1pmc$2 digitaldaemon.com...
 "Rupert Millard" <rupertamillard hotmail.dot.com> wrote in message
 news:bgnto9$qnv$2 digitaldaemon.com...
 Hi all!

 Does D sport inner classes / structs in any form?

 For some reason, I assumed it did and have got into some trouble!




 way
 I have at least one bug to report. If it is intended to have some


 for these could someone explain what ways members of the outer class




 be
 accessed?

D supports nested classes, where the scopes are nested. It does not

 java-style 'inner' classes. If you've found a bug, please post it here!

begin 666 bug.d M+R\ 0E5'. T*+R\ ;F5S=&5D(&-L87-S97, 9&]N)W0 8V]R<F5C=&QY(&%C M8V5S<R!M96UB97)S(&]F(&]U=&5R(&-L87-S#0HO+R!#3U!-4$E,15(Z#0HO M+R!$:6=I=&%L($UA<G, 1"!#;VUP:6QE<B!"971A('8P+C8X#0H-"FEM<&]R M="!C+G-T9&EO.PT*#0IC;&%S<R!!('L-" EI;G0 >#L-" T*"71H:7,H:6YT M(' R*0T*"7L-" D)>#UX,CL-" E]#0H)#0H)<')I=F%T92!S=')U8W0 0B![ M#0H)"0T*"0EV;VED(&1I<W!L87E8(" I#0H)"7L-" D)"7!R:6YT9B B:6X M0BYD:7-P;&%Y6" I.EQN( T*"0D)(" (" (")X.B E=5QN(BP >"D[#0H) M"7T-" E]#0H-" EP<FEV871E('9O:60 9&ES<&QA>5 H*0T*"7L-" D)0B!B M.PT*"0D-" D)8BYD:7-P;&%Y6" I.PT*"7T-"GT-" T*:6YT(&UA:6X *&-H M87);75M=(&%R9W,I#0I[#0H)02!A(#T ;F5W($$H-CDI.PT*"0T*"7!R:6YT M9B B:6X ;6%I;B I.EQN( T*"2 (" (" B>#H )74 7&Y<;B(L(&$N>"D[ E#0H)#0H)82YD:7-P;&%Y6" I.PT*"0T*"7)E='5R;B P.PT*?0`` ` end


Aug 06 2003
parent reply Gennadi Pais <Gennadi_member pathlink.com> writes:
Rupert,

You could see the example of variable access from inner class in function (not
in outer class) in the D spec: "Member functions of nested classes and structs
do not have access to the stack variables of the enclosing function, but do have
access to the other symbols". So I think that is the same logical decision.
In the question of compilation error I agree with.

Thanks!

In article <bgqsqc$lvm$1 digitaldaemon.com>, Rupert Millard says...
Gennadi,

Thanks for the link.

I don't see the relevance of nested functions to the problem in question,
however the manual says "Nested functions have access to the variables and
other symbols defined by the lexically enclosing function." If one were to
infer anything about nested classes from this, wouldn't it be that a nested
class could access the members of the enclosing class?

If what you go on to say is the case then shouldn't the compiler generate an
error (as it would do if B were a class instead of a struct) rather than
compiling the code?

From,

Rupert

"Gennadi Pais" <Gennadi_member pathlink.com> wrote in message
news:bgqq03$jgj$1 digitaldaemon.com...
 Hi Rupert,

 for the Java-style inner class explanation you can read the next link:
 http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
 Hope you'll find that is brief and clear.

 About the "bug" you arose I think that is not a bug at all. Pay attention

 explanation about nested function that described either on the page 100 of

 spec in pdf pormat or go to the http://www.digitalmars.com/d/function.html

 When you declare (and define) a nested class in another one you just

 scope of that class, not a variable or some kind of instance that is

 to concrete instance ot outer class. So an inner class can't receive a

 non-static member of outer one, have no access to its frame.

 Hope I've succeeded to explain the "bug" behavior.
 Thanks!

 In article <bgqm0m$g2h$1 digitaldaemon.com>, Rupert Millard says...
Walter,

I doubt this code should be allowed to compile as a struct is nested


a class, however it does compile and it produces this output on my


when executed:

in main():
x: 69

in B.displayX():
x: 4202672

I believe this behaviour is a bug.

As I was not able to find any help in the manual, could you explain


what kind of nesting is allowed and what it permits you to do?


what does nested scopes mean?

Also, I understand that there is some ambiguity in the term 'inner


in JAVA. Could somebody clarify exactly what it means?

Please excuse my ignorance, I am an entirely self-taught programmer doing


as a hobby.

Thanks,

From,

Rupert


"Walter" <walter digitalmars.com> wrote in message
news:bgorth$1pmc$2 digitaldaemon.com...
 "Rupert Millard" <rupertamillard hotmail.dot.com> wrote in message
 news:bgnto9$qnv$2 digitaldaemon.com...
 Hi all!

 Does D sport inner classes / structs in any form?

 For some reason, I assumed it did and have got into some trouble!




 way
 I have at least one bug to report. If it is intended to have some


 for these could someone explain what ways members of the outer class




 be
 accessed?

D supports nested classes, where the scopes are nested. It does not

 java-style 'inner' classes. If you've found a bug, please post it here!

begin 666 bug.d M+R\ 0E5'. T*+R\ ;F5S=&5D(&-L87-S97, 9&]N)W0 8V]R<F5C=&QY(&%C M8V5S<R!M96UB97)S(&]F(&]U=&5R(&-L87-S#0HO+R!#3U!-4$E,15(Z#0HO M+R!$:6=I=&%L($UA<G, 1"!#;VUP:6QE<B!"971A('8P+C8X#0H-"FEM<&]R M="!C+G-T9&EO.PT*#0IC;&%S<R!!('L-" EI;G0 >#L-" T*"71H:7,H:6YT M(' R*0T*"7L-" D)>#UX,CL-" E]#0H)#0H)<')I=F%T92!S=')U8W0 0B![ M#0H)"0T*"0EV;VED(&1I<W!L87E8(" I#0H)"7L-" D)"7!R:6YT9B B:6X M0BYD:7-P;&%Y6" I.EQN( T*"0D)(" (" (")X.B E=5QN(BP >"D[#0H) M"7T-" E]#0H-" EP<FEV871E('9O:60 9&ES<&QA>5 H*0T*"7L-" D)0B!B M.PT*"0D-" D)8BYD:7-P;&%Y6" I.PT*"7T-"GT-" T*:6YT(&UA:6X *&-H M87);75M=(&%R9W,I#0I[#0H)02!A(#T ;F5W($$H-CDI.PT*"0T*"7!R:6YT M9B B:6X ;6%I;B I.EQN( T*"2 (" (" B>#H )74 7&Y<;B(L(&$N>"D[ E#0H)#0H)82YD:7-P;&%Y6" I.PT*"0T*"7)E='5R;B P.PT*?0`` ` end



Aug 06 2003
parent "Rupert Millard" <rupertamillard hotmail.dot.com> writes:
Gennadi,

Well spotted! Thanks very much for all your help.

From,

Rupert

"Gennadi Pais" <Gennadi_member pathlink.com> wrote in message
news:bgqumc$nop$1 digitaldaemon.com...
 Rupert,

 You could see the example of variable access from inner class in function

 in outer class) in the D spec: "Member functions of nested classes and

 do not have access to the stack variables of the enclosing function, but

 access to the other symbols". So I think that is the same logical

 In the question of compilation error I agree with.

 Thanks!

 In article <bgqsqc$lvm$1 digitaldaemon.com>, Rupert Millard says...
Gennadi,

Thanks for the link.

I don't see the relevance of nested functions to the problem in question,
however the manual says "Nested functions have access to the variables


other symbols defined by the lexically enclosing function." If one were


infer anything about nested classes from this, wouldn't it be that a


class could access the members of the enclosing class?

If what you go on to say is the case then shouldn't the compiler generate


error (as it would do if B were a class instead of a struct) rather than
compiling the code?

From,

Rupert

"Gennadi Pais" <Gennadi_member pathlink.com> wrote in message
news:bgqq03$jgj$1 digitaldaemon.com...
 Hi Rupert,

 for the Java-style inner class explanation you can read the next link:
 http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
 Hope you'll find that is brief and clear.

 About the "bug" you arose I think that is not a bug at all. Pay



to
 explanation about nested function that described either on the page 100



the D
 spec in pdf pormat or go to the



 When you declare (and define) a nested class in another one you just

 scope of that class, not a variable or some kind of instance that is

 to concrete instance ot outer class. So an inner class can't receive a

 non-static member of outer one, have no access to its frame.

 Hope I've succeeded to explain the "bug" behavior.
 Thanks!

 In article <bgqm0m$g2h$1 digitaldaemon.com>, Rupert Millard says...
Walter,

I doubt this code should be allowed to compile as a struct is nested


a class, however it does compile and it produces this output on my


when executed:

in main():
x: 69

in B.displayX():
x: 4202672

I believe this behaviour is a bug.

As I was not able to find any help in the manual, could you explain


what kind of nesting is allowed and what it permits you to do?


what does nested scopes mean?

Also, I understand that there is some ambiguity in the term 'inner


in JAVA. Could somebody clarify exactly what it means?

Please excuse my ignorance, I am an entirely self-taught programmer




it
as a hobby.

Thanks,

From,

Rupert


"Walter" <walter digitalmars.com> wrote in message
news:bgorth$1pmc$2 digitaldaemon.com...
 "Rupert Millard" <rupertamillard hotmail.dot.com> wrote in message
 news:bgnto9$qnv$2 digitaldaemon.com...
 Hi all!

 Does D sport inner classes / structs in any form?

 For some reason, I assumed it did and have got into some trouble!




 way
 I have at least one bug to report. If it is intended to have some


 for these could someone explain what ways members of the outer






can
 be
 accessed?

D supports nested classes, where the scopes are nested. It does not

 java-style 'inner' classes. If you've found a bug, please post it





begin 666 bug.d
M+R\ 0E5'. T*+R\ ;F5S=&5D(&-L87-S97, 9&]N)W0 8V]R<F5C=&QY(&%C
M8V5S<R!M96UB97)S(&]F(&]U=&5R(&-L87-S#0HO+R!#3U!-4$E,15(Z#0HO
M+R!$:6=I=&%L($UA<G, 1"!#;VUP:6QE<B!"971A('8P+C8X#0H-"FEM<&]R
M="!C+G-T9&EO.PT*#0IC;&%S<R!!('L-" EI;G0 >#L-" T*"71H:7,H:6YT
M(' R*0T*"7L-" D)>#UX,CL-" E]#0H)#0H)<')I=F%T92!S=')U8W0 0B![
M#0H)"0T*"0EV;VED(&1I<W!L87E8(" I#0H)"7L-" D)"7!R:6YT9B B:6X 
M0BYD:7-P;&%Y6" I.EQN( T*"0D)("  ("  (")X.B E=5QN(BP >"D[#0H)
M"7T-" E]#0H-" EP<FEV871E('9O:60 9&ES<&QA>5 H*0T*"7L-" D)0B!B
M.PT*"0D-" D)8BYD:7-P;&%Y6" I.PT*"7T-"GT-" T*:6YT(&UA:6X *&-H
M87);75M=(&%R9W,I#0I[#0H)02!A(#T ;F5W($$H-CDI.PT*"0T*"7!R:6YT
M9B B:6X ;6%I;B I.EQN( T*"2  ("  (" B>#H )74 7&Y<;B(L(&$N>"D[
E#0H)#0H)82YD:7-P;&%Y6" I.PT*"0T*"7)E='5R;B P.PT*?0``
`
end




Aug 06 2003
prev sibling parent reply BenjiSmith <BenjiSmith_member pathlink.com> writes:
As a former Java guy, I would personally RATHER have inner classes than nested
classes. As far as I can tell, nested classes only limit the SCOPE of the class,
which can occasionally be somewhat useful. But inner classes allow the inner
class to talk to the outer class, which can OFTEN be REALLY useful.

I'd love to see inner classes in D.

--Benji Smith
Aug 06 2003
parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
I've so often wished I could do that.

I'd like to see some kind of inner class support as well.

Sean

"BenjiSmith" <BenjiSmith_member pathlink.com> wrote in message
news:bgr1m8$qmc$1 digitaldaemon.com...
 As a former Java guy, I would personally RATHER have inner classes than

 classes. As far as I can tell, nested classes only limit the SCOPE of the

 which can occasionally be somewhat useful. But inner classes allow the

 class to talk to the outer class, which can OFTEN be REALLY useful.

 I'd love to see inner classes in D.

 --Benji Smith

Aug 06 2003
parent reply "Matthew Wilson" <matthew stlsoft.org> writes:
Can we not have both?

I've found nested classes very useful in C++, essential in many ways to
enforce design decisions and control users of one's classes.

I've found inner classes very useful in Java (along with those groovy
anonymous gizmos)

Let's have both in D!

"Sean L. Palmer" <palmer.sean verizon.net> wrote in message
news:bgrcff$1563$1 digitaldaemon.com...
 I've so often wished I could do that.

 I'd like to see some kind of inner class support as well.

 Sean

 "BenjiSmith" <BenjiSmith_member pathlink.com> wrote in message
 news:bgr1m8$qmc$1 digitaldaemon.com...
 As a former Java guy, I would personally RATHER have inner classes than

 classes. As far as I can tell, nested classes only limit the SCOPE of


 class,
 which can occasionally be somewhat useful. But inner classes allow the

 class to talk to the outer class, which can OFTEN be REALLY useful.

 I'd love to see inner classes in D.

 --Benji Smith


Aug 06 2003
parent "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"Matthew Wilson" <matthew stlsoft.org> wrote in message
news:bgs4et$1s2p$1 digitaldaemon.com...
 Can we not have both?

 I've found nested classes very useful in C++, essential in many ways to
 enforce design decisions and control users of one's classes.

 I've found inner classes very useful in Java (along with those groovy
 anonymous gizmos)

 Let's have both in D!

 "Sean L. Palmer" <palmer.sean verizon.net> wrote in message
 news:bgrcff$1563$1 digitaldaemon.com...
 I've so often wished I could do that.

 I'd like to see some kind of inner class support as well.

 Sean


I though we'd been through this b4, and Walter was totally against inner classes, becuase inner functions as delgates can (in many places) replace them. however if we're back on that, I too vote for inner classes akin to Java, they are more robust than inner functions as delegates (and infact you could make a delegate of a fuction of an anon innerclass that was a shallow copy of the inner functions stack frame, thus full closures). I would like to see one addition to inner classes, that of allowing an inner class to be subclassed without having to subclass the outer class. as in class Base { inner class Item { // I've used inner here as in Java it would be class, but in D class means Java static class ... } } in Java to have a subclass of Base.Item you need to do class Derived : Base { inner class MyItem : Base.Item { } } because MyItem needs an instance of Base to be connected to; I would like to see class MyItem : Item.Base { // assumes that MyItem is connected to a Base } to create an instance of a MyClass use something like Base b = new Base(); MyItem ii = b.new MyItem(); or even class Derived : Base { } Derived d = new Derived(); MyItem ii = d.new MyItem();
Aug 07 2003