digitalmars.D - Opt-out closures
- Sean Eskapp (11/11) Jan 23 2011 I get errors when working with nested functions and structs or scoped cl...
- Jonathan M Davis (5/18) Jan 23 2011 If a nested function is marked as static, then it results in a function ...
- Sean Eskapp (4/22) Jan 23 2011 I want to be able to access the enclosing scope, but NOT after the funct...
- bearophile (7/10) Jan 23 2011 It seems a worth thing to ask for. A possible syntax (not currently supp...
- Sean Eskapp (2/12) Jan 23 2011 That looks like one of the best options syntax-wise. Nice!
- Andrew Wiley (4/15) Jan 23 2011 I don't like being too negative, but if we're removing "scope" as a stor...
- Jacob Carlborg (4/14) Jan 23 2011 Doesn't D2 have scoped delegates, at last as a parameter to a function?
- Torarin (13/16) Jan 23 2011 Until we have a dedicated syntax for it, I think you can use this hack:
- Sean Eskapp (2/19) Jan 23 2011 Yup, works great with everything I've tried! Thanks.
- bearophile (8/11) Jan 23 2011 I have written that without too much thinking, so I'm sure that's not a ...
I get errors when working with nested functions and structs or scoped classes, because closures can't be used with anything with scoped destruction. This makes complete sense, but I don't even want the closure functionality of these nested functions. Personally, I would like to be able to opt-out of the closure functionality of nested functions: if the enclosing function exits, and a nested function thereof is called, then a segfault would occur when it tried to access the stack of its enclosing function; however, access of scoped-destruction variables from an enclosing-scope function would be fine. Thoughts? The not-being-able-to-access-scoped-destruction-variables thing is really getting to me, since one of the main driving features I like about D is the ability to use anonymous and nested functions.
Jan 23 2011
On Sunday 23 January 2011 06:36:27 Sean Eskapp wrote:I get errors when working with nested functions and structs or scoped classes, because closures can't be used with anything with scoped destruction. This makes complete sense, but I don't even want the closure functionality of these nested functions. Personally, I would like to be able to opt-out of the closure functionality of nested functions: if the enclosing function exits, and a nested function thereof is called, then a segfault would occur when it tried to access the stack of its enclosing function; however, access of scoped-destruction variables from an enclosing-scope function would be fine. Thoughts? The not-being-able-to-access-scoped-destruction-variables thing is really getting to me, since one of the main driving features I like about D is the ability to use anonymous and nested functions.If a nested function is marked as static, then it results in a function rather than a delegate. Of course, that means that you can't access the enclosing scope, but if you don't care about that, then just use static. - Jonathan M Davis
Jan 23 2011
== Quote from Jonathan M Davis (jmdavisProg gmx.com)'s articleOn Sunday 23 January 2011 06:36:27 Sean Eskapp wrote:I want to be able to access the enclosing scope, but NOT after the function has exited; I should have the option of accessing the enclosing scope, but at the cost of making my delegate not a closure.I get errors when working with nested functions and structs or scoped classes, because closures can't be used with anything with scoped destruction. This makes complete sense, but I don't even want the closure functionality of these nested functions. Personally, I would like to be able to opt-out of the closure functionality of nested functions: if the enclosing function exits, and a nested function thereof is called, then a segfault would occur when it tried to access the stack of its enclosing function; however, access of scoped-destruction variables from an enclosing-scope function would be fine. Thoughts? The not-being-able-to-access-scoped-destruction-variables thing is really getting to me, since one of the main driving features I like about D is the ability to use anonymous and nested functions.If a nested function is marked as static, then it results in a function rather than a delegate. Of course, that means that you can't access the enclosing scope, but if you don't care about that, then just use static. - Jonathan M Davis
Jan 23 2011
Sean Eskapp:I want to be able to access the enclosing scope, but NOT after the function has exited; I should have the option of accessing the enclosing scope, but at the cost of making my delegate not a closure.It seems a worth thing to ask for. A possible syntax (not currently supported): void foo() { scope void bar() {} } Bye, bearophile
Jan 23 2011
== Quote from bearophile (bearophileHUGS lycos.com)'s articleSean Eskapp:That looks like one of the best options syntax-wise. Nice!I want to be able to access the enclosing scope, but NOT after the function has exited; I should have the option of accessing the enclosing scope, but at the cost of making my delegate not a closure.It seems a worth thing to ask for. A possible syntax (not currently supported): void foo() { scope void bar() {} } Bye, bearophile
Jan 23 2011
On Sun, Jan 23, 2011 at 9:08 AM, bearophile <bearophileHUGS lycos.com>wrote:Sean Eskapp:I don't like being too negative, but if we're removing "scope" as a storage class because it can leave unsafe dangling pointers, how likely is it that we'll get delegates that can have dangling pointers?I want to be able to access the enclosing scope, but NOT after thefunction hasexited; I should have the option of accessing the enclosing scope, but atthe costof making my delegate not a closure.It seems a worth thing to ask for. A possible syntax (not currently supported): void foo() { scope void bar() {} }
Jan 23 2011
On 2011-01-23 16:08, bearophile wrote:Sean Eskapp:Doesn't D2 have scoped delegates, at last as a parameter to a function? -- /Jacob CarlborgI want to be able to access the enclosing scope, but NOT after the function has exited; I should have the option of accessing the enclosing scope, but at the cost of making my delegate not a closure.It seems a worth thing to ask for. A possible syntax (not currently supported): void foo() { scope void bar() {} } Bye, bearophile
Jan 23 2011
2011/1/23 Sean Eskapp <eatingstaples gmail.com>:I want to be able to access the enclosing scope, but NOT after the function has exited; I should have the option of accessing the enclosing scope, but at the cost of making my delegate not a closure.Until we have a dedicated syntax for it, I think you can use this hack: import std.traits; auto scopeDelegate(D)(scope D d) if (isDelegate!D) { return d; } int main() { StructWithDtor s; trustedFunction(scopeDelegate({s.a = 5;}); // No heap allocation } Torarin
Jan 23 2011
== Quote from Torarin (torarind gmail.com)'s article2011/1/23 Sean Eskapp <eatingstaples gmail.com>:Yup, works great with everything I've tried! Thanks.I want to be able to access the enclosing scope, but NOT after the function has exited; I should have the option of accessing the enclosing scope, but at the cost of making my delegate not a closure.Until we have a dedicated syntax for it, I think you can use this hack: import std.traits; auto scopeDelegate(D)(scope D d) if (isDelegate!D) { return d; } int main() { StructWithDtor s; trustedFunction(scopeDelegate({s.a = 5;}); // No heap allocation } Torarin
Jan 23 2011
Andrew Wiley:I don't like being too negative, but if we're removing "scope" as a storage class because it can leave unsafe dangling pointers, how likely is it that we'll get delegates that can have dangling pointers?I have written that without too much thinking, so I'm sure that's not a perfect idea :-) Regarding "scope" for classes, it is deprecated, instead of improving it, but the currently alternative solution is _less_ safe than the scope, and it's more buggy: http://d.puremagic.com/issues/show_bug.cgi?id=5115 A Java VM like the Oracle one has the best GC, plus escape analysis that allows it to stack-allocate some class instances. Currently D compilers have a far worse GC and even LDC-LLVM escape analysis is weak and it doesn't do much for D class instances. In my opinion D and its compilers need to perform that stack optimization by themselves, or the programmer needs to be "empowered" with some good, efficient and as much safe as possible way to stack allocate a class instance. At the moment D has none of both. Good luck with a slow system language :-) Bye, bearophile
Jan 23 2011