digitalmars.D.learn - How to use structs for RAII?
- Sean Eskapp (3/3) Jan 22 2011 It was recommended to me to use structs for RAII instead of scope classe...
- bearophile (4/7) Jan 22 2011 Inside the ~this() you may put code, to deallocate resources you have al...
- Sean Eskapp (4/11) Jan 22 2011 By explicit constructor, I assume you mean one with parameters? I guess ...
- Andrej Mitrovic (22/22) Jan 22 2011 A workaround:
- Sean Eskapp (1/1) Jan 22 2011 What's the difference between assert and enforce in D?
- Robert Clipsham (25/26) Jan 22 2011 Use assert() for something that must almost be true, regardless of user
- Andrej Mitrovic (2/28) Jan 22 2011
- Sean Eskapp (1/1) Jan 22 2011 Ah, that clears it up. Thanks!
- Jacob Carlborg (4/26) Jan 23 2011 A static opCall would be better. See my answer to the original post.
- Andrej Mitrovic (1/1) Jan 23 2011 Ah, I keep forgetting about opCall. Nice tips there.
- Sean Eskapp (3/3) Jan 22 2011 Actually this becomes rather annoying, since I can't use any closures on...
- Jonathan M Davis (15/23) Jan 22 2011 The typical thing to do when you want a default constructor for a struct...
- Jacob Carlborg (14/17) Jan 23 2011 You can use a static opCall, like this:
It was recommended to me to use structs for RAII instead of scope classes, since scope is being removed (?). However, since default-constructors for structs can't exist, how does one do this?
Jan 22 2011
Sean Eskapp:It was recommended to me to use structs for RAII instead of scope classes, since scope is being removed (?). However, since default-constructors for structs can't exist, how does one do this?Inside the ~this() you may put code, to deallocate resources you have allocated in an explicit constructor (or in some creation method). Is this enough? Bye, bearophile
Jan 22 2011
== Quote from bearophile (bearophileHUGS lycos.com)'s articleSean Eskapp:in an explicit constructor (or in some creation method). Is this enough?It was recommended to me to use structs for RAII instead of scope classes, since scope is being removed (?). However, since default-constructors for structs can't exist, how does one do this?Inside the ~this() you may put code, to deallocate resources you have allocatedBye, bearophileBy explicit constructor, I assume you mean one with parameters? I guess that's alright.
Jan 22 2011
A workaround: import std.stdio; import std.exception; struct A { int x; this(void* none) { if (none !is null) { enforce(0, "Tried to pass a parameter to A's constructor"); } writeln("in constructor"); // construction from here.. x = 5; } } void main() { auto a = A(null); } I think that would be safe, and closest to a "default" constructor.
Jan 22 2011
What's the difference between assert and enforce in D?
Jan 22 2011
On 23/01/11 00:33, Sean Eskapp wrote:What's the difference between assert and enforce in D?Use assert() for something that must almost be true, regardless of user input, or anything else. enforce() on the other hand is for things which may or may not be true. The source code should clear things up: ---- T enforce(T)(T value, lazy Throwable ex) { if (!value) throw ex(); return value; } ---- So it's essentially just shorthand for throwing an exception. Example: ---- void main(string[] args) { enforce(args.length > 2, "The user needs to pass the correct parameters"); assert(1+1==2, "If this isn't true, the application should terminate"); } ---- -- Robert http://octarineparrot.com/
Jan 22 2011
assert also disappears when building in release mode. On 1/23/11, Robert Clipsham <robert octarineparrot.com> wrote:On 23/01/11 00:33, Sean Eskapp wrote:What's the difference between assert and enforce in D?Use assert() for something that must almost be true, regardless of user input, or anything else. enforce() on the other hand is for things which may or may not be true. The source code should clear things up: ---- T enforce(T)(T value, lazy Throwable ex) { if (!value) throw ex(); return value; } ---- So it's essentially just shorthand for throwing an exception. Example: ---- void main(string[] args) { enforce(args.length > 2, "The user needs to pass the correct parameters"); assert(1+1==2, "If this isn't true, the application should terminate"); } ---- -- Robert http://octarineparrot.com/
Jan 22 2011
On 2011-01-23 01:14, Andrej Mitrovic wrote:A workaround: import std.stdio; import std.exception; struct A { int x; this(void* none) { if (none !is null) { enforce(0, "Tried to pass a parameter to A's constructor"); } writeln("in constructor"); // construction from here.. x = 5; } } void main() { auto a = A(null); } I think that would be safe, and closest to a "default" constructor.A static opCall would be better. See my answer to the original post. -- /Jacob Carlborg
Jan 23 2011
Ah, I keep forgetting about opCall. Nice tips there.
Jan 23 2011
Actually this becomes rather annoying, since I can't use any closures on the object. Stupidly, I can still use closures with an object which is deleted at the end of the function.
Jan 22 2011
On Saturday 22 January 2011 15:09:29 bearophile wrote:Sean Eskapp:The typical thing to do when you want a default constructor for a struct is to use a static opCall(). Then, for struct S, you'd do auto s = S(); If you did S s(), it still wouldn't work, so you might want to add an assertion in the destructor which verifes that the struct wasn't constructor with S.init, in which case what the destructor would be doing would likely be wrong. Personally, I _always_ declare struct variables in this form auto s = S(); and never as S s(); It avoids most of the problem with a struct's init value. But you can still get structs initialized to their init when they're in arrays and the like (which is a good part of the reason that init exists in the first place). - Jonathan M DavisIt was recommended to me to use structs for RAII instead of scope classes, since scope is being removed (?). However, since default-constructors for structs can't exist, how does one do this?Inside the ~this() you may put code, to deallocate resources you have allocated in an explicit constructor (or in some creation method). Is this enough?
Jan 22 2011
On 2011-01-23 00:03, Sean Eskapp wrote:It was recommended to me to use structs for RAII instead of scope classes, since scope is being removed (?). However, since default-constructors for structs can't exist, how does one do this?You can use a static opCall, like this: struct Foo { static Foo opCall () { Foo foo; // initialize foo return foo; } } auto foo = Foo(); -- /Jacob Carlborg
Jan 23 2011