digitalmars.D - Who's using structs nested in functions?
- Andrei Alexandrescu (14/14) Oct 22 2009 Refer to:
- Denis Koroskin (8/22) Oct 22 2009 Everything you can do with nested struct is doable with a nested class a...
- bearophile (5/10) Oct 22 2009 I have used nested static structs sometimes, when I need a struct just i...
- Jeremie Pelletier (8/22) Oct 22 2009 I've had similar uses, some win32 api routines require custom structs
- bearophile (4/7) Oct 22 2009 That's why I have said "static nested structs", they are like nested str...
- Fawzi Mohamed (6/33) Oct 22 2009 I use structs in nested functions as context for parallel recursive
- Andrei Alexandrescu (4/41) Oct 22 2009 Could you prepend "static" in front of their definition and still have
- Fawzi Mohamed (5/48) Oct 23 2009 Sure, I thought nested structures were always static, and did not have
- Fawzi Mohamed (4/55) Oct 23 2009 Ok I see that indeed in D2.0 struct have a pointer to the context, no I
- Andrei Alexandrescu (3/17) Oct 22 2009 I'm asking about non-static nested structs.
- Michel Fortin (12/30) Oct 22 2009 I tried implementing things using nested structs in my mfr.xml
Refer to: http://www.digitalmars.com/d/2.0/struct.html and scroll down to the last section, "Nested Structs". A struct defined inside a function has a hidden pointer to that function's stack frame and therefore can use function's local variables. Nested classes do a similar trick, but for those there's a bit of motivation - you could create a nested class and use it as a sort of closure by returning a base class or interface of it. With nested structs, however, you can't do much. You can pass them to a template, but I can't see some solid use cases there. My understanding is that nested structs have been implemented for completeness and consistency with nested classes. Any good example of nested struct uses? Andrei
Oct 22 2009
On Thu, 22 Oct 2009 18:07:15 +0400, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:Refer to: http://www.digitalmars.com/d/2.0/struct.html and scroll down to the last section, "Nested Structs". A struct defined inside a function has a hidden pointer to that function's stack frame and therefore can use function's local variables. Nested classes do a similar trick, but for those there's a bit of motivation - you could create a nested class and use it as a sort of closure by returning a base class or interface of it. With nested structs, however, you can't do much. You can pass them to a template, but I can't see some solid use cases there. My understanding is that nested structs have been implemented for completeness and consistency with nested classes. Any good example of nested struct uses? AndreiEverything you can do with nested struct is doable with a nested class as well (marking methods as final, using scope to allocate instance on stack etc). I don't think there's anything specific to nested struct. One thing I can think of is that you are using some template that works with structs only, but then you may create a nested class and nest a static struct inside it (not that I would recommend doing so but still) :)
Oct 22 2009
Andrei Alexandrescu:With nested structs, however, you can't do much. You can pass them to a template, but I can't see some solid use cases there. My understanding is that nested structs have been implemented for completeness and consistency with nested classes. Any good example of nested struct uses?I have used nested static structs sometimes, when I need a struct just inside a function (like the main()) to avoid polluting the outer scope with the struct name. Do you want to remove them from D2? Bye, bearophile
Oct 22 2009
bearophile wrote:Andrei Alexandrescu:I've had similar uses, some win32 api routines require custom structs like BITMAPINFO, nested structs are neat to declare the struct right before its only usage. However I don't think having a closure for that struct is really needed, nested functions already perform that task very well, and I use those quite often. JeremieWith nested structs, however, you can't do much. You can pass them to a template, but I can't see some solid use cases there. My understanding is that nested structs have been implemented for completeness and consistency with nested classes. Any good example of nested struct uses?I have used nested static structs sometimes, when I need a struct just inside a function (like the main()) to avoid polluting the outer scope with the struct name. Do you want to remove them from D2? Bye, bearophile
Oct 22 2009
Jeremie Pelletier:However I don't think having a closure for that struct is really needed, nested functions already perform that task very well, and I use those quite often.That's why I have said "static nested structs", they are like nested structs, but they don't have the extra field. Bye, bearophile
Oct 22 2009
On 2009-10-22 16:33:01 +0200, Jeremie Pelletier <jeremiep gmail.com> said:bearophile wrote:I use structs in nested functions as context for parallel recursive loops, actually my code would be nicer if circular referring structs would be allowed in nested functions (at the moment this is not possible, even with forward references). FawziAndrei Alexandrescu:I've had similar uses, some win32 api routines require custom structs like BITMAPINFO, nested structs are neat to declare the struct right before its only usage. However I don't think having a closure for that struct is really needed, nested functions already perform that task very well, and I use those quite often. JeremieWith nested structs, however, you can't do much. You can pass them to a template, but I can't see some solid use cases there. My understanding is that nested structs have been implemented for completeness and consistency with nested classes. Any good example of nested struct uses?I have used nested static structs sometimes, when I need a struct just inside a function (like the main()) to avoid polluting the outer scope with the struct name. Do you want to remove them from D2? Bye, bearophile
Oct 22 2009
Fawzi Mohamed wrote:On 2009-10-22 16:33:01 +0200, Jeremie Pelletier <jeremiep gmail.com> said:Could you prepend "static" in front of their definition and still have them work? Andreibearophile wrote:I use structs in nested functions as context for parallel recursive loops, actually my code would be nicer if circular referring structs would be allowed in nested functions (at the moment this is not possible, even with forward references). FawziAndrei Alexandrescu:I've had similar uses, some win32 api routines require custom structs like BITMAPINFO, nested structs are neat to declare the struct right before its only usage. However I don't think having a closure for that struct is really needed, nested functions already perform that task very well, and I use those quite often. JeremieWith nested structs, however, you can't do much. You can pass them to a template, but I can't see some solid use cases there. My understanding is that nested structs have been implemented for completeness and consistency with nested classes. Any good example of nested struct uses?I have used nested static structs sometimes, when I need a struct just inside a function (like the main()) to avoid polluting the outer scope with the struct name. Do you want to remove them from D2? Bye, bearophile
Oct 22 2009
On 2009-10-22 18:11:01 +0200, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> said:Fawzi Mohamed wrote:Sure, I thought nested structures were always static, and did not have access to the context, as classes do. FawziOn 2009-10-22 16:33:01 +0200, Jeremie Pelletier <jeremiep gmail.com> said:Could you prepend "static" in front of their definition and still have them work? Andreibearophile wrote:I use structs in nested functions as context for parallel recursive loops, actually my code would be nicer if circular referring structs would be allowed in nested functions (at the moment this is not possible, even with forward references). FawziAndrei Alexandrescu:I've had similar uses, some win32 api routines require custom structs like BITMAPINFO, nested structs are neat to declare the struct right before its only usage. However I don't think having a closure for that struct is really needed, nested functions already perform that task very well, and I use those quite often. JeremieWith nested structs, however, you can't do much. You can pass them to a template, but I can't see some solid use cases there. My understanding is that nested structs have been implemented for completeness and consistency with nested classes. Any good example of nested struct uses?I have used nested static structs sometimes, when I need a struct just inside a function (like the main()) to avoid polluting the outer scope with the struct name. Do you want to remove them from D2? Bye, bearophile
Oct 23 2009
On 2009-10-23 12:11:20 +0200, Fawzi Mohamed <fmohamed mac.com> said:On 2009-10-22 18:11:01 +0200, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> said:Ok I see that indeed in D2.0 struct have a pointer to the context, no I don't use that. FawziFawzi Mohamed wrote:Sure, I thought nested structures were always static, and did not have access to the context, as classes do. FawziOn 2009-10-22 16:33:01 +0200, Jeremie Pelletier <jeremiep gmail.com> said:Could you prepend "static" in front of their definition and still have them work? Andreibearophile wrote:I use structs in nested functions as context for parallel recursive loops, actually my code would be nicer if circular referring structs would be allowed in nested functions (at the moment this is not possible, even with forward references). FawziAndrei Alexandrescu:I've had similar uses, some win32 api routines require custom structs like BITMAPINFO, nested structs are neat to declare the struct right before its only usage. However I don't think having a closure for that struct is really needed, nested functions already perform that task very well, and I use those quite often. JeremieWith nested structs, however, you can't do much. You can pass them to a template, but I can't see some solid use cases there. My understanding is that nested structs have been implemented for completeness and consistency with nested classes. Any good example of nested struct uses?I have used nested static structs sometimes, when I need a struct just inside a function (like the main()) to avoid polluting the outer scope with the struct name. Do you want to remove them from D2? Bye, bearophile
Oct 23 2009
bearophile wrote:Andrei Alexandrescu:I'm asking about non-static nested structs. AndreiWith nested structs, however, you can't do much. You can pass them to a template, but I can't see some solid use cases there. My understanding is that nested structs have been implemented for completeness and consistency with nested classes. Any good example of nested struct uses?I have used nested static structs sometimes, when I need a struct just inside a function (like the main()) to avoid polluting the outer scope with the struct name. Do you want to remove them from D2? Bye, bearophile
Oct 22 2009
On 2009-10-22 10:07:15 -0400, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> said:Refer to: http://www.digitalmars.com/d/2.0/struct.html and scroll down to the last section, "Nested Structs". A struct defined inside a function has a hidden pointer to that function's stack frame and therefore can use function's local variables. Nested classes do a similar trick, but for those there's a bit of motivation - you could create a nested class and use it as a sort of closure by returning a base class or interface of it. With nested structs, however, you can't do much. You can pass them to a template, but I can't see some solid use cases there. My understanding is that nested structs have been implemented for completeness and consistency with nested classes. Any good example of nested struct uses?I tried implementing things using nested structs in my mfr.xml module[1] but, for some reasons I don't remember anymore (template issues?), I wasn't too successful and ended up creating anonymous scope classes. All this because of the lack of nested overloaded functions. [1]: see: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=98861 -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Oct 22 2009