July 12, 2010 Re: Recommended way to do RAII cleanly | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 13.07.2010 00:09, bearophile wrote:
> Jonathan M Davis:
>> There are lots of cases where using scope(exit) makes sense, and it's a great
>> construct. But there are also plenty of cases where using plain old RAII with a
>> single declaration is better. It works fine in D as long as the struct in
>> question doesn't need a default constructor. But if it does, then it becomes a
>> problem.
>
> Can't you use a static opCall (also suggested by Jacob Carlborg)?
>
> Bye,
> bearophile
I think that's supposed to go away, but I'm not 100% sure. Would make sense, since it was added as a sort of stopgap measure when there were no struct literals.
|
July 12, 2010 Re: Recommended way to do RAII cleanly | ||||
---|---|---|---|---|
| ||||
Posted in reply to torhu | On Monday, July 12, 2010 15:40:24 torhu wrote:
> On 13.07.2010 00:09, bearophile wrote:
> > Jonathan M Davis:
> >> There are lots of cases where using scope(exit) makes sense, and it's a
> >> great construct. But there are also plenty of cases where using plain
> >> old RAII with a single declaration is better. It works fine in D as
> >> long as the struct in question doesn't need a default constructor. But
> >> if it does, then it becomes a problem.
> >
> > Can't you use a static opCall (also suggested by Jacob Carlborg)?
> >
> > Bye,
> > bearophile
>
> I think that's supposed to go away, but I'm not 100% sure. Would make sense, since it was added as a sort of stopgap measure when there were no struct literals.
This would be why I like Berophile's suggestion on the main list of having a page which lists deprecated and intended to be deprecated constructs. opCall is a good solution and basically fulfills the requirements of a default constructor, but if it's going away, then it's in the same camp as using scope on a class.
- Jonathan M Davis
|
July 12, 2010 Re: Recommended way to do RAII cleanly | ||||
---|---|---|---|---|
| ||||
Posted in reply to torhu | torhu:
> I think that's supposed to go away, but I'm not 100% sure. Would make sense, since it was added as a sort of stopgap measure when there were no struct literals.
Well, if you have found a use case for static opCall then it needs to be shown to the people that want to deprecate the static opCall, to ask for ways to replace its functionality.
Bye,
bearophile
|
July 13, 2010 Re: Recommended way to do RAII cleanly | ||||
---|---|---|---|---|
| ||||
Posted in reply to torhu | "torhu" <no@spam.invalid> wrote in message news:i1ft84$2h4j$1@digitalmars.com... > > I think the conclusion is that RAII is less important in D than in C++. In D you use scope (exit), or even finally, like in Java or Python. The other use of scope, as a storage class, is supposed to go away, and I suspect I'm not the only one who's going to miss it. As good as scope guards are, anytime you have an object that has some sort of cleanup function that needs to be called when you're done with it, it's absurd to think that requiring the *user* of the object to use scope guards *by convention* is ever a satisfactory substitute for real RAII. And I have to say, I'm rather disappointed to hear that scope objects are going away. What is the reason for that? |
July 13, 2010 Re: Recommended way to do RAII cleanly | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | On Monday, July 12, 2010 18:15:04 Nick Sabalausky wrote:
> "torhu" <no@spam.invalid> wrote in message news:i1ft84$2h4j$1@digitalmars.com...
>
> > I think the conclusion is that RAII is less important in D than in C++. In D you use scope (exit), or even finally, like in Java or Python. The other use of scope, as a storage class, is supposed to go away, and I suspect I'm not the only one who's going to miss it.
>
> As good as scope guards are, anytime you have an object that has some sort of cleanup function that needs to be called when you're done with it, it's absurd to think that requiring the *user* of the object to use scope guards *by convention* is ever a satisfactory substitute for real RAII.
>
> And I have to say, I'm rather disappointed to hear that scope objects are going away. What is the reason for that?
IIRC, Walter was of the opinion that you could do the same with structs without needing scope. And as long as you can default construct them or your RAII object requires arguments to its constructor, that's more or less true (if arguably limiting). The question, therefore is whether there is a means to effectively default construct structs which is not going to be deprecated. I'd still like scope to stick around (even if all it did was put the class on the heap as normal and called clear() on it when it left scope rather than putting it on the stack like I believe it does at the moment), but if structs can totally take care of the RAII issue, then it's not as bad. But the situation with structs and default constructors is a bit frustrating.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation