Jump to page: 1 2
Thread overview
How to use structs for RAII?
Jan 22, 2011
Sean Eskapp
Jan 22, 2011
bearophile
Jan 22, 2011
Sean Eskapp
Jan 23, 2011
Andrej Mitrovic
assert vs enforce?
Jan 23, 2011
Sean Eskapp
Jan 23, 2011
Robert Clipsham
Jan 23, 2011
Andrej Mitrovic
Jan 23, 2011
Sean Eskapp
Jan 23, 2011
Jacob Carlborg
Jan 23, 2011
Andrej Mitrovic
Jan 23, 2011
Sean Eskapp
Jan 23, 2011
Jonathan M Davis
Jan 23, 2011
Jacob Carlborg
January 22, 2011
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?
January 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
January 22, 2011
== Quote from bearophile (bearophileHUGS@lycos.com)'s article
> 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

By explicit constructor, I assume you mean one with parameters? I guess that's alright.
January 23, 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.
January 23, 2011
What's the difference between assert and enforce in D?
January 23, 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/
January 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.
January 23, 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/
>
January 23, 2011
Ah, that clears it up. Thanks!
January 23, 2011
On Saturday 22 January 2011 15:09:29 bearophile wrote:
> 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?

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 Davis
« First   ‹ Prev
1 2