digitalmars.D - auto used as scope
- Martin Persenius <martin persenius.net> Apr 18 2007
- Lionello Lunesu <lio lunesu.remove.com> Apr 18 2007
- Manfred Nowak <svv1999 hotmail.com> Apr 18 2007
- Martin Persenius <martin persenius.net> Apr 21 2007
- Dan <murpsoft hotmail.com> Apr 21 2007
- Manfred Nowak <svv1999 hotmail.com> Apr 21 2007
Hi!
There is inconsistency in the usage of 'auto', where it actually behaves as
'scope'. According to users of #d this might be because auto comes from scope.
I would like to understand why the dtor is and is not called in the following
cases, and the reasoning for having it that way.
import std.stdio;
class Foo {
this() {
writef("0");
}
~this() {
writef("1");
}
}
int main() {
try {
scope(failure) writef("4");
scope(exit) writef("2");
auto Foo f = new Foo();
throw new Exception("msg");
scope(exit) writef("9");
scope(success) writef("8");
scope(failure) writef("7");
}
catch (Exception e)
writef("%s", e.toString);
writefln();
try {
scope(failure) writef("4");
scope(exit) writef("2");
Foo f = new Foo();
throw new Exception("msg");
scope(exit) writef("9");
scope(success) writef("8");
scope(failure) writef("7");
}
catch (Exception e)
writef("%s", e.toString);
writefln();
try {
scope(failure) writef("4");
scope(exit) writef("2");
auto Foo = new Foo();
throw new Exception("msg");
scope(exit) writef("9");
scope(success) writef("8");
scope(failure) writef("7");
}
catch (Exception e)
writef("%s", e.toString);
writefln();
return 0;
}
Output:
0124msg
024msg
024msg
The spec page for this is here:
http://www.digitalmars.com/d/statement.html#ScopeGuardStatement
"If any auto instances are to be destructed upon the close of the scope, they
also are interleaved with the ScopeGuardStatements in the reverse lexical order
in which they appear."
Apr 18 2007
Martin Persenius wrote:Hi! There is inconsistency in the usage of 'auto', where it actually behaves as 'scope'. According to users of #d this might be because auto comes from scope. I would like to understand why the dtor is and is not called in the following cases, and the reasoning for having it that way. import std.stdio; class Foo { this() { writef("0"); } ~this() { writef("1"); } } int main() { try { scope(failure) writef("4"); scope(exit) writef("2"); auto Foo f = new Foo(); throw new Exception("msg");
Perhaps the writef does some allocation that will detect the disappearance of any pointer to your Foo, thus collecting the object and running its dtor? It sounds probable, the first time writef is run it could allocate some buffer. L.
Apr 18 2007
Martin Persenius wroteauto Foo = new Foo();
You have redefined Foo to be a variable name. http://www.digitalmars.com/webnews/newsgroups.php? art_group=digitalmars.D&article_id=43970 -manfred
Apr 18 2007
Manfred Nowak Wrote:Martin Persenius wroteauto Foo = new Foo();
You have redefined Foo to be a variable name. http://www.digitalmars.com/webnews/newsgroups.php? art_group=digitalmars.D&article_id=43970 -manfred
Right, thank you. But what is the rationale behind making the first case call the dtor as if "auto [classname] [variablename]" was using 'scope'? The others, including the quoted example, do not, This is inconsistent and non-intuitive. Just want to understand.
Apr 21 2007
Martin Persenius Wrote:Right, thank you. But what is the rationale behind making the first case call the dtor as if "auto [classname] [variablename]" was using 'scope'? The others, including the quoted example, do not, This is inconsistent and non-intuitive. Just want to understand.
I think it means that: 'auto' - the type is automatically detected. 'scope' - the data is stored on the stack, and will be lost with the scope.
Apr 21 2007
Martin Persenius wroteas if "auto [classname] [variablename]" was using 'scope'?
Look into the docs please. Seems that this usage of "auto" will be deprecated soon and replaced by "scope". -manfred
Apr 21 2007









Lionello Lunesu <lio lunesu.remove.com> 