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
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?
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
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?
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!
begin 666 bug.d M+R\ 0E5'. T*+R\ ;F5S=&5D(&-L87-S97, 9&]N)W0 8V]R<F5C=&QY(&%C M(' R*0T*"7L-" D)>#UX M.PT*"0D-" D)8BYD:7-P;&%Y6" I.PT*"7T-"GT-" T*:6YT(&UA:6X *&-H M9B B:6X ;6%I;B I.EQN( T*"2 (" (" B>#H )74 7&Y<;B(L(&$N>"D[ ` end
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
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?
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!
begin 666 bug.d M+R\ 0E5'. T*+R\ ;F5S=&5D(&-L87-S97, 9&]N)W0 8V]R<F5C=&QY(&%C M(' R*0T*"7L-" D)>#UX M.PT*"0D-" D)8BYD:7-P;&%Y6" I.PT*"7T-"GT-" T*:6YT(&UA:6X *&-H M9B B:6X ;6%I;B I.EQN( T*"2 (" (" B>#H )74 7&Y<;B(L(&$N>"D[ ` 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
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
 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?
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!
begin 666 bug.d M+R\ 0E5'. T*+R\ ;F5S=&5D(&-L87-S97, 9&]N)W0 8V]R<F5C=&QY(&%C M(' R*0T*"7L-" D)>#UX M.PT*"0D-" D)8BYD:7-P;&%Y6" I.PT*"7T-"GT-" T*:6YT(&UA:6X *&-H M9B B:6X ;6%I;B I.EQN( T*"2 (" (" B>#H )74 7&Y<;B(L(&$N>"D[ ` 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
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
 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?
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!
begin 666 bug.d M+R\ 0E5'. T*+R\ ;F5S=&5D(&-L87-S97, 9&]N)W0 8V]R<F5C=&QY(&%C M(' R*0T*"7L-" D)>#UX M.PT*"0D-" D)8BYD:7-P;&%Y6" I.PT*"7T-"GT-" T*:6YT(&UA:6X *&-H M9B B:6X ;6%I;B I.EQN( T*"2 (" (" B>#H )74 7&Y<;B(L(&$N>"D[ ` 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
(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
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
 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?
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!
begin 666 bug.d
M+R\ 0E5'. T*+R\ ;F5S=&5D(&-L87-S97, 9&]N)W0 8V]R<F5C=&QY(&%C



M(' R*0T*"7L-" D)>#UX



M.PT*"0D-" D)8BYD:7-P;&%Y6" I.PT*"7T-"GT-" T*:6YT(&UA:6X *&-H

M9B B:6X ;6%I;B I.EQN( T*"2  ("  (" B>#H )74 7&Y<;B(L(&$N>"D[

`
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
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 "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
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 "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