digitalmars.D - auto used as scope
- Martin Persenius (57/57) Apr 18 2007 Hi!
- Lionello Lunesu (6/28) Apr 18 2007 Perhaps the writef does some allocation that will detect the
- Manfred Nowak (5/6) Apr 18 2007 You have redefined Foo to be a variable name.
- Martin Persenius (2/12) Apr 21 2007 Right, thank you. But what is the rationale behind making the first case...
- Dan (4/5) Apr 21 2007 I think it means that:
- Manfred Nowak (4/5) Apr 21 2007 Look into the docs please. Seems that this usage of "auto" will be
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 wroteRight, 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.auto 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 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