Thread overview
auto delegate
Aug 02, 2005
Ben Hinkle
Aug 02, 2005
Sean Kelly
Aug 03, 2005
Ben Hinkle
Aug 03, 2005
Nod
August 02, 2005
The ScopeExit auto class I posted
http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.bugs/4674 made me
think of the following enhancement request for 'auto'. Currently the auto
attribute can be applied to a delegate but it is ignored:
class Foo {
  void close() {printf("closing\n");}
}
int main() {
  Foo f = new Foo;
  {
    auto void delegate() dg = &f.close;
    printf("before scope exit\n");
  }
  printf("after scope exit\n");
  return 0;
}

produces

before scope exit
after scope exit

The enhancement request is to make a delegate that is declared as 'auto' run at the scope exit. That way the ScopeExit auto class wouldn't be needed and we wouldn't have to worry about allocating auto objects on the stack or heap since the delegate is stored on the stack. That would allow a few auto classes to be removed and probably most auto classes wouldn't be needed.


August 02, 2005
In article <dcou74$2s3b$1@digitaldaemon.com>, Ben Hinkle says...
>
>The ScopeExit auto class I posted
>http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.bugs/4674 made me
>think of the following enhancement request for 'auto'. Currently the auto
>attribute can be applied to a delegate but it is ignored:
>class Foo {
>  void close() {printf("closing\n");}
>}
>int main() {
>  Foo f = new Foo;
>  {
>    auto void delegate() dg = &f.close;
>    printf("before scope exit\n");
>  }
>  printf("after scope exit\n");
>  return 0;
>}
>
>produces
>
>before scope exit
>after scope exit
>
>The enhancement request is to make a delegate that is declared as 'auto' run at the scope exit. That way the ScopeExit auto class wouldn't be needed and we wouldn't have to worry about allocating auto objects on the stack or heap since the delegate is stored on the stack. That would allow a few auto classes to be removed and probably most auto classes wouldn't be needed.

This is a fantastic idea.  The vast bulk of auto classes I'd use could be replaced by auto delegates.  And it would get around the awkward use of _alloca to boot.


Sean


August 03, 2005
"Sean Kelly" <sean@f4.ca> wrote in message news:dcouvb$2sh5$1@digitaldaemon.com...
> In article <dcou74$2s3b$1@digitaldaemon.com>, Ben Hinkle says...
>>
>>The ScopeExit auto class I posted
>>http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.bugs/4674 made me
>>think of the following enhancement request for 'auto'. Currently the auto
>>attribute can be applied to a delegate but it is ignored:
>>class Foo {
>>  void close() {printf("closing\n");}
>>}
>>int main() {
>>  Foo f = new Foo;
>>  {
>>    auto void delegate() dg = &f.close;
>>    printf("before scope exit\n");
>>  }
>>  printf("after scope exit\n");
>>  return 0;
>>}
>>
>>produces
>>
>>before scope exit
>>after scope exit
>>
>>The enhancement request is to make a delegate that is declared as 'auto'
>>run
>>at the scope exit. That way the ScopeExit auto class wouldn't be needed
>>and
>>we wouldn't have to worry about allocating auto objects on the stack or
>>heap
>>since the delegate is stored on the stack. That would allow a few auto
>>classes to be removed and probably most auto classes wouldn't be needed.
>
> This is a fantastic idea.  The vast bulk of auto classes I'd use could be
> replaced by auto delegates.  And it would get around the awkward use of
> _alloca
> to boot.

cool. The only arguments I can think of against it are that
1) it is not RAII - to which the answer is that in D RAII is implemented by
small one-off auto classes since large auto classes are too restricted
2) try/finally is similar - to which the answer is that an auto delegate is
"cleaner" and it lets you put the action where you want (presumably right
before or after the acquisition)

ps - I should say the ScopedLock is due to Sean.


August 03, 2005
In article <dcou74$2s3b$1@digitaldaemon.com>, Ben Hinkle says...
>
>The ScopeExit auto class I posted
>http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.bugs/4674 made me
>think of the following enhancement request for 'auto'. Currently the auto
>attribute can be applied to a delegate but it is ignored:
>class Foo {
>  void close() {printf("closing\n");}
>}
>int main() {
>  Foo f = new Foo;
>  {
>    auto void delegate() dg = &f.close;
>    printf("before scope exit\n");
>  }
>  printf("after scope exit\n");
>  return 0;
>}
>
>produces
>
>before scope exit
>after scope exit
>
>The enhancement request is to make a delegate that is declared as 'auto' run at the scope exit. That way the ScopeExit auto class wouldn't be needed and we wouldn't have to worry about allocating auto objects on the stack or heap since the delegate is stored on the stack. That would allow a few auto classes to be removed and probably most auto classes wouldn't be needed.
>
>

Interesting idea, I like it. It is indeed cleaner than try/finally, keeping cleanup code more in context.

Your idea gets me thinking of a more generalized version though, or rather a 'sweeter' one, allowing any code to be cued for scope exit. With defined order of execution, of course.

Example:
fd = open(...);
auto { close(fd); }

I think I saw something similar in the Perl 6 spec, allowing LAST{} blocks to be placed inside any scope... I thought that was a good idea too, though using a D attribute is cleaner, imo.

-Nod-