www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Inner class

reply Geert <gertje gertje.org> writes:
What's an inner class and how can it be usefull??
May 28 2005
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Geert" <gertje gertje.org> wrote in message 
news:d7avhu$1gnn$1 digitaldaemon.com...
 What's an inner class and how can it be usefull??

Currently, D's "nested" classes are really no different from declaring classes separate. Something like: class A { class B { } } Gives you no advantage over declaring B outside A. (BTW, when the new inner class functionality appears, you will have to write "static class B" to get this old functionality). Inner classes are kind of analogous to nested functions. You can make a small, private inner class to handle some aspects of a larger class when you don't want anything else to access that smaller class. The outer and inner classes can access each other's methods and data, so they're very tightly coupled. Some people say that delegates are better. I don't understand this argument, as it is assuming the position that inner classes are used _solely_ for the purpose of event listeners. The fact is that you can use inner classes for whatever you want, and there are circumstances when you need the inner thing to be able to hold data as well, something that delegates just can't do. There are also some other cool tricks you can do with inner classes, such as overloading its opIndex/opIndexApply and making it look, to external functions, like an array ;)
May 29 2005
next sibling parent Geert <gertje gertje.org> writes:
Thanks, I allready read the posts in the d.D group, but this has helped 
alot!


Jarrett Billingsley wrote:
 "Geert" <gertje gertje.org> wrote in message 
 news:d7avhu$1gnn$1 digitaldaemon.com...
 
What's an inner class and how can it be usefull??

Currently, D's "nested" classes are really no different from declaring classes separate. Something like: class A { class B { } } Gives you no advantage over declaring B outside A. (BTW, when the new inner class functionality appears, you will have to write "static class B" to get this old functionality). Inner classes are kind of analogous to nested functions. You can make a small, private inner class to handle some aspects of a larger class when you don't want anything else to access that smaller class. The outer and inner classes can access each other's methods and data, so they're very tightly coupled. Some people say that delegates are better. I don't understand this argument, as it is assuming the position that inner classes are used _solely_ for the purpose of event listeners. The fact is that you can use inner classes for whatever you want, and there are circumstances when you need the inner thing to be able to hold data as well, something that delegates just can't do. There are also some other cool tricks you can do with inner classes, such as overloading its opIndex/opIndexApply and making it look, to external functions, like an array ;)

May 29 2005
prev sibling next sibling parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
 such as overloading its opIndex/opIndexApply and making
 it look, to external functions, like an array ;)

Why wait? You can use structures for that right now. Implementation of such proxy objects using classes, inner or not, is just waste of memory which will follow to its fragmentation. Example which I am using - proxy of opApply: struct Array(T) { T[] elements; struct ENUM // bidirectional enumerator { T[] _a; bool _forward; int opApply(int delegate(inout T) dg) { int result = 0; if(_forward) for(uint i = 0; i < _a.length; ++i ) { T t = _a[i]; result = dg(t); if (result) break; } else for(int i = _a.length - 1; i >= 0; --i ) { T t = _a[i]; result = dg(t); if (result) break; } return result; } } ENUM items(bool forward) { ENUM w; w._forward = forward; w._a = elements; return w; } ENUM forward() { return items(true); } ENUM backward() { return items(false); } } ----------------- Use: Array!(int) myarr; .... foreach(int i; myarr.forward) { .... } "Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message news:d7chjd$2jjf$1 digitaldaemon.com...
 "Geert" <gertje gertje.org> wrote in message 
 news:d7avhu$1gnn$1 digitaldaemon.com...
 What's an inner class and how can it be usefull??

Currently, D's "nested" classes are really no different from declaring classes separate. Something like: class A { class B { } } Gives you no advantage over declaring B outside A. (BTW, when the new inner class functionality appears, you will have to write "static class B" to get this old functionality). Inner classes are kind of analogous to nested functions. You can make a small, private inner class to handle some aspects of a larger class when you don't want anything else to access that smaller class. The outer and inner classes can access each other's methods and data, so they're very tightly coupled. Some people say that delegates are better. I don't understand this argument, as it is assuming the position that inner classes are used _solely_ for the purpose of event listeners. The fact is that you can use inner classes for whatever you want, and there are circumstances when you need the inner thing to be able to hold data as well, something that delegates just can't do. There are also some other cool tricks you can do with inner classes, such as overloading its opIndex/opIndexApply and making it look, to external functions, like an array ;)

May 29 2005
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Jarrett Billingsley wrote:
<snip>
 Some people say that delegates are better.  I don't understand this 
 argument, as it is assuming the position that inner classes are used 
 _solely_ for the purpose of event listeners.  The fact is that you can use 
 inner classes for whatever you want, and there are circumstances when you 
 need the inner thing to be able to hold data as well, something that 
 delegates just can't do.

I'm still rather sceptical about the benefits of inner classes. Can they do anything that can't be done with a static nested class? digitalmars.D/25020
 There are also some other cool tricks you can do 
 with inner classes, such as overloading its opIndex/opIndexApply and making 
 it look, to external functions, like an array ;) 

We can already do this. What would an inner class add to this functionality? Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jun 02 2005