digitalmars.D - classes inside functions?!
- Hasan Aljudy (48/48) Apr 30 2006 I don't know if this is a bug or what, but for some reason, dmd allows
- Sean Kelly (4/13) Apr 30 2006 It is. Templates are merely a special case as they must have external
- kellywilson nowhere.com (12/25) Apr 30 2006 Hey guys,
- Hasan Aljudy (3/42) Apr 30 2006 I'm sort of working on my own parser ;)
- Bruno Medeiros (9/25) May 01 2006 What do you mean by that? What is external linkage? Did you meant an
- Sean Kelly (9/29) May 01 2006 External linkage means that a symbol is visible from another module.
- Don Clugston (4/33) May 02 2006 There's some weird stuff in there. Mixins can be local, and because of
- Bruno Medeiros (16/50) May 03 2006 There are no templates *defined* local to functions. Did you mean mixin
- Don Clugston (8/56) May 03 2006 Curiously, this isn't the whole story. There's a mangled name (difficult...
- Bruno Medeiros (12/67) May 04 2006 Hum curious indeed, I wonder what it's for.
- Bruno Medeiros (5/37) May 03 2006 I see. Hum, can a local variable (local to a function) be external?
- Sean Kelly (21/23) May 03 2006 Sort of. A static local variable has external linkage in D (and I think...
I don't know if this is a bug or what, but for some reason, dmd allows you to define classes/structs/unions/enums as statements inside function bodies. Oddly enough, you cannot define a template in the same way I didn't find in the docs any mention of whether aggregates are allowed inside function bodies or not. Walter, is this the correct behaviour? The following compiles ok void main() { enum X { A, B, } struct Y { int x; int y; char a; } class R { Y g; X d; } } but the following doesn't work void main() { class R { Y g; X d; } template kill(X) { void kill() { } } } gives the following error messeges:
Apr 30 2006
Hasan Aljudy wrote:I don't know if this is a bug or what, but for some reason, dmd allows you to define classes/structs/unions/enums as statements inside function bodies. Oddly enough, you cannot define a template in the same way I didn't find in the docs any mention of whether aggregates are allowed inside function bodies or not. Walter, is this the correct behaviour?It is. Templates are merely a special case as they must have external linkage. Sean
Apr 30 2006
Hey guys, My parser (based on 0.149) allows the first example and rejects the second. Thus the grammar that I have seems to confirm what Sean has said, as well. If anyone wants a copy of the pre-release (based on the Elkhound GLR parser generator) then just email me at: wilsonk-at-cpsc-dot-ucalgary-dot-ca Thanks, Kelly Wilson P.S. Ivan (or BCS), if you want an update then please email (I have fixed about 7 or 8 problems from the original...though there are still some minor things to clean up). In article <e321m5$100h$1 digitaldaemon.com>, Sean Kelly says...Hasan Aljudy wrote:I don't know if this is a bug or what, but for some reason, dmd allows you to define classes/structs/unions/enums as statements inside function bodies. Oddly enough, you cannot define a template in the same way I didn't find in the docs any mention of whether aggregates are allowed inside function bodies or not. Walter, is this the correct behaviour?It is. Templates are merely a special case as they must have external linkage. Sean
Apr 30 2006
kellywilson nowhere.com wrote:Hey guys, My parser (based on 0.149) allows the first example and rejects the second. Thus the grammar that I have seems to confirm what Sean has said, as well. If anyone wants a copy of the pre-release (based on the Elkhound GLR parser generator) then just email me at: wilsonk-at-cpsc-dot-ucalgary-dot-caI'm sort of working on my own parser ;) btw I study at UofC too!!Thanks, Kelly Wilson P.S. Ivan (or BCS), if you want an update then please email (I have fixed about 7 or 8 problems from the original...though there are still some minor things to clean up). In article <e321m5$100h$1 digitaldaemon.com>, Sean Kelly says...Hasan Aljudy wrote:I don't know if this is a bug or what, but for some reason, dmd allows you to define classes/structs/unions/enums as statements inside function bodies. Oddly enough, you cannot define a template in the same way I didn't find in the docs any mention of whether aggregates are allowed inside function bodies or not. Walter, is this the correct behaviour?It is. Templates are merely a special case as they must have external linkage. Sean
Apr 30 2006
Sean Kelly wrote:Hasan Aljudy wrote:What do you mean by that? What is external linkage? Did you meant an extern(al) storage class? (I just found out now something I unknew, that are two distinct "extern" keywords/concepts in D) And how exactly does that restricts templates not being allowed inside functions? -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#DI don't know if this is a bug or what, but for some reason, dmd allows you to define classes/structs/unions/enums as statements inside function bodies. Oddly enough, you cannot define a template in the same way I didn't find in the docs any mention of whether aggregates are allowed inside function bodies or not. Walter, is this the correct behaviour?It is. Templates are merely a special case as they must have external linkage. Sean
May 01 2006
Bruno Medeiros wrote:Sean Kelly wrote:External linkage means that a symbol is visible from another module. But I mis-spoke. Template parameters must have external linkage. Templates themselves may only be defined at module or class scope. This is a carryover from C++, and I'm not certain whether there's a technical obstacle to function-scope templates or not. There doesn't seem to be, but then I've never implemented template support in a compiler before. Perhaps Walter could explain further. SeanHasan Aljudy wrote:What do you mean by that? What is external linkage? Did you meant an extern(al) storage class? (I just found out now something I unknew, that are two distinct "extern" keywords/concepts in D) And how exactly does that restricts templates not being allowed inside functions?I don't know if this is a bug or what, but for some reason, dmd allows you to define classes/structs/unions/enums as statements inside function bodies. Oddly enough, you cannot define a template in the same way I didn't find in the docs any mention of whether aggregates are allowed inside function bodies or not. Walter, is this the correct behaviour?It is. Templates are merely a special case as they must have external linkage.
May 01 2006
Sean Kelly wrote:Bruno Medeiros wrote:There's some weird stuff in there. Mixins can be local, and because of alias template parameters, there's a name mangling for templates which are defined local to functions.Sean Kelly wrote:External linkage means that a symbol is visible from another module. But I mis-spoke. Template parameters must have external linkage. Templates themselves may only be defined at module or class scope. This is a carryover from C++, and I'm not certain whether there's a technical obstacle to function-scope templates or not. There doesn't seem to be, but then I've never implemented template support in a compiler before. Perhaps Walter could explain further.Hasan Aljudy wrote:What do you mean by that? What is external linkage? Did you meant an extern(al) storage class? (I just found out now something I unknew, that are two distinct "extern" keywords/concepts in D) And how exactly does that restricts templates not being allowed inside functions?I don't know if this is a bug or what, but for some reason, dmd allows you to define classes/structs/unions/enums as statements inside function bodies. Oddly enough, you cannot define a template in the same way I didn't find in the docs any mention of whether aggregates are allowed inside function bodies or not. Walter, is this the correct behaviour?It is. Templates are merely a special case as they must have external linkage.
May 02 2006
Don Clugston wrote:Sean Kelly wrote:There are no templates *defined* local to functions. Did you mean mixin instantiations? I think (normal) template instances are quite different from mixin instances in that regard. Mixin instances have copy-paste behaviour and are each unique, and the mixin name part has no name-magling at all in the case of an anonymous mixin, or has the name which is defined in the named mixin. (Normal) template instances have name-mangling dependent on the template arguments, because that is what _identifies_ the template instance. There is no such parallel with mixins, as they are unique. I can't test any of these suppositions yet, as I can't check the .obj's symbols or ASM (I'm planning to get the EUP soon), but I'm fairly sure it's something more or less like this. -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#DBruno Medeiros wrote:There's some weird stuff in there. Mixins can be local, and because of alias template parameters, there's a name mangling for templates which are defined local to functions.Sean Kelly wrote:External linkage means that a symbol is visible from another module. But I mis-spoke. Template parameters must have external linkage. Templates themselves may only be defined at module or class scope. This is a carryover from C++, and I'm not certain whether there's a technical obstacle to function-scope templates or not. There doesn't seem to be, but then I've never implemented template support in a compiler before. Perhaps Walter could explain further.Hasan Aljudy wrote:What do you mean by that? What is external linkage? Did you meant an extern(al) storage class? (I just found out now something I unknew, that are two distinct "extern" keywords/concepts in D) And how exactly does that restricts templates not being allowed inside functions?I don't know if this is a bug or what, but for some reason, dmd allows you to define classes/structs/unions/enums as statements inside function bodies. Oddly enough, you cannot define a template in the same way I didn't find in the docs any mention of whether aggregates are allowed inside function bodies or not. Walter, is this the correct behaviour?It is. Templates are merely a special case as they must have external linkage.
May 03 2006
Bruno Medeiros wrote:Don Clugston wrote:Yes, sorry.Sean Kelly wrote:There are no templates *defined* local to functions. Did you mean mixin instantiations?Bruno Medeiros wrote:There's some weird stuff in there. Mixins can be local, and because of alias template parameters, there's a name mangling for templates which are defined local to functions.Sean Kelly wrote:External linkage means that a symbol is visible from another module. But I mis-spoke. Template parameters must have external linkage. Templates themselves may only be defined at module or class scope. This is a carryover from C++, and I'm not certain whether there's a technical obstacle to function-scope templates or not. There doesn't seem to be, but then I've never implemented template support in a compiler before. Perhaps Walter could explain further.Hasan Aljudy wrote:What do you mean by that? What is external linkage? Did you meant an extern(al) storage class? (I just found out now something I unknew, that are two distinct "extern" keywords/concepts in D) And how exactly does that restricts templates not being allowed inside functions?I don't know if this is a bug or what, but for some reason, dmd allows you to define classes/structs/unions/enums as statements inside function bodies. Oddly enough, you cannot define a template in the same way I didn't find in the docs any mention of whether aggregates are allowed inside function bodies or not. Walter, is this the correct behaviour?It is. Templates are merely a special case as they must have external linkage.I think (normal) template instances are quite different from mixin instances in that regard. Mixin instances have copy-paste behaviour and are each unique, and the mixin name part has no name-magling at all in the case of an anonymous mixin, or has the name which is defined in the named mixin.Curiously, this isn't the whole story. There's a mangled name (difficult to access) which includes the name of the function that the mixin is defined in, and it includes the template arguments that were used -- it's almost the whole mechanism that you'd expect for templates which are local to functions. It's completely undocumented, of course. (Normal) template instances have name-mangling dependent onthe template arguments, because that is what _identifies_ the template instance. There is no such parallel with mixins, as they are unique. I can't test any of these suppositions yet, as I can't check the .obj's symbols or ASM (I'm planning to get the EUP soon), but I'm fairly sure it's something more or less like this.
May 03 2006
Don Clugston wrote:Bruno Medeiros wrote:Hum curious indeed, I wonder what it's for. In any case I don't think there is any problem in creating a mangled name from a local alias argument. As long as an argument has a fully-qualified name, it can be mangled I believe, and even local variables have FQNs. The restriction is likely instead that a (normal)template instance simply cannot access the frame of the parent function of the argument (just like the error message says), so a local argument can't be used. -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#DDon Clugston wrote:Yes, sorry.Sean Kelly wrote:There are no templates *defined* local to functions. Did you mean mixin instantiations?Bruno Medeiros wrote:There's some weird stuff in there. Mixins can be local, and because of alias template parameters, there's a name mangling for templates which are defined local to functions.Sean Kelly wrote:External linkage means that a symbol is visible from another module. But I mis-spoke. Template parameters must have external linkage. Templates themselves may only be defined at module or class scope. This is a carryover from C++, and I'm not certain whether there's a technical obstacle to function-scope templates or not. There doesn't seem to be, but then I've never implemented template support in a compiler before. Perhaps Walter could explain further.Hasan Aljudy wrote:What do you mean by that? What is external linkage? Did you meant an extern(al) storage class? (I just found out now something I unknew, that are two distinct "extern" keywords/concepts in D) And how exactly does that restricts templates not being allowed inside functions?I don't know if this is a bug or what, but for some reason, dmd allows you to define classes/structs/unions/enums as statements inside function bodies. Oddly enough, you cannot define a template in the same way I didn't find in the docs any mention of whether aggregates are allowed inside function bodies or not. Walter, is this the correct behaviour?It is. Templates are merely a special case as they must have external linkage.I think (normal) template instances are quite different from mixin instances in that regard. Mixin instances have copy-paste behaviour and are each unique, and the mixin name part has no name-magling at all in the case of an anonymous mixin, or has the name which is defined in the named mixin.Curiously, this isn't the whole story. There's a mangled name (difficult to access) which includes the name of the function that the mixin is defined in, and it includes the template arguments that were used -- it's almost the whole mechanism that you'd expect for templates which are local to functions. It's completely undocumented, of course.
May 04 2006
Sean Kelly wrote:Bruno Medeiros wrote:I see. Hum, can a local variable (local to a function) be external? -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#DSean Kelly wrote:External linkage means that a symbol is visible from another module. But I mis-spoke. Template parameters must have external linkage. Templates themselves may only be defined at module or class scope. This is a carryover from C++, and I'm not certain whether there's a technical obstacle to function-scope templates or not. There doesn't seem to be, but then I've never implemented template support in a compiler before. Perhaps Walter could explain further. SeanHasan Aljudy wrote:What do you mean by that? What is external linkage? Did you meant an extern(al) storage class? (I just found out now something I unknew, that are two distinct "extern" keywords/concepts in D) And how exactly does that restricts templates not being allowed inside functions?I don't know if this is a bug or what, but for some reason, dmd allows you to define classes/structs/unions/enums as statements inside function bodies. Oddly enough, you cannot define a template in the same way I didn't find in the docs any mention of whether aggregates are allowed inside function bodies or not. Walter, is this the correct behaviour?It is. Templates are merely a special case as they must have external linkage.
May 03 2006
Bruno Medeiros wrote:I see. Hum, can a local variable (local to a function) be external?Sort of. A static local variable has external linkage in D (and I think in C++ as well, though the meaning of 'static' is a bit different there). Here's an example: import std.c.stdio; template templ( alias A ) { void templ() { printf( "%i\n", A ); } } int i = 5; void main() { templ!(i); int j = 6; templ!(j); } Change the "int j = 6" to "static int j = 6" and everything compiles. Sean
May 03 2006