Re: Equivalent to Python with Statement
On Wednesday, 28 February 2018 at 22:00:11 UTC, meppl wrote: On Wednesday, 28 February 2018 at 21:47:40 UTC, Cym13 wrote: On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote: [...] Others have discussed that particular case at length, but to provide a more generic answer the correct way to translate a python context manager is to use scope(out): { // Scope restriction similar to with's block auto f = File("x.txt"); scope(out) f.close(); // closes f on scope exit no matter what /* do stuff with f */ } This accurately reproduces the behaviour of python's with although it's less automatic. what is `scope(out)`? I only know these file:///usr/share/dmd-doc/html/d/spec/statement.html#ScopeGuardStatement sorry, my question was not good. I thought there is an undocumented feature, never mind ;)
Re: Equivalent to Python with Statement
On Wednesday, 28 February 2018 at 21:47:40 UTC, Cym13 wrote: On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote: [...] Others have discussed that particular case at length, but to provide a more generic answer the correct way to translate a python context manager is to use scope(out): { // Scope restriction similar to with's block auto f = File("x.txt"); scope(out) f.close(); // closes f on scope exit no matter what /* do stuff with f */ } This accurately reproduces the behaviour of python's with although it's less automatic. what is `scope(out)`? I only know these file:///usr/share/dmd-doc/html/d/spec/statement.html#ScopeGuardStatement
Re: Return value in BetterC mode.
On Saturday, 17 February 2018 at 13:47:28 UTC, meppl wrote: On Saturday, 17 February 2018 at 07:58:40 UTC, ANtlord wrote: ... ... sadly I have no good idea how to name the title of that issue :/ I looked at it again and came up with a title name: https://issues.dlang.org/show_bug.cgi?id=18457
Re: Return value in BetterC mode.
On Saturday, 17 February 2018 at 07:58:40 UTC, ANtlord wrote: Hello! Yesterday I found an interesting issue for myself while I was implementing unique pointer for my project in BetterC. When I was trying to return new instance from a `move` method I got calling of destructor the instance. When the instance is out of scope the calling is happens again. I also see that addresses of an instance that calls destructor are same. That thing doesn't happen when I don't use BetterC mode. Is that bug or feature? Please checkout code https://run.dlang.io/is/m1TRnT and try it in BetterC mode and in usual mode. But if I return result of a constructor directly instead of saving the result to variable and returning the variable from the `move` method I don't see double calling of the same destructor. I. e. ``` auto newObject = Box(this); return newObject; ``` is replaced by ``` return Box(this); ``` So... is that bug or feature? Okay, with the glorious help of the compiler-switch `-vcg-ast` I was able to see the source of that weirdness. See also: https://forum.dlang.org/thread/juxihybpqpjycmxiy...@forum.dlang.org It seems that there is always a try-block when the struct has a destructor I reduced your code to: struct S { ~this() {} } S myFunction() { S s = S(); return s; } extern( C) void main(){} transformation of myFunction() with $ dmd -betterC -vcg-ast source/app.d S myFunction() { S s = S(); try { return s; } finally s.~this(); } transformation of myFunction() with $ dmd -vcg-ast source/app.d S myFunction() { S s = S(); try return s; catch(Throwable __o3) { s.~this(); throw __o3; } } sadly I have no good idea how to name the title of that issue :/