Deterministic resource freeing - delete deprecation (again)

KennyTM~ kennytm at gmail.com
Wed Apr 27 05:52:32 PDT 2011


On Apr 27, 11 19:42, Mariusz Gliwiński wrote:
> Hello,
> I'm next person, which isn't necessarily happy about delete operator
> deprecation.

I don't get the recent resistant against deletion of 'delete' which has 
been decided months before (before TDPL). The operator is removed does 
not mean the destructor can't be invoked at all.

> Because constructors / destructors are frequently used not only for
> application controlled memory management, how would You implement
> something like following code without delete operator?
>

Use 'scoped' or a 'struct' if you want to constrain 's1' in a scope

import std.typecons;
void main(string[] args) {
     auto res = new Resource();
     {
         auto s1 = scoped!FirstSystem(res);
     }
     auto s2 = new SecondSystem(res);
}

<or>

void main(string[] args) {
     auto res = new Resource();
     {
         auto s1 = FirstSystem(res);
     }
     auto s2 = new SecondSystem(res);
}
struct FirstSystem {
     private Resource res;
     this(Resource res) {
         this.res = res;
         res.referenced = true;
     }
     ~this() {
         res.referenced=false;
     }
}
class Resource {
     public bool referenced;
}

Or use 'clear' to replace 'delete' as told in many posts already

void main(string[] args) {
     auto res = new Resource();
     auto s1 = new FirstSystem(res);
     // many miles away
     clear(s1);
     auto s2 = new SecondSystem(res);
}

> <code>
[snip]
> </code>
>
> Next questions would be:
> * Are you going to drop destructor as well, or maybe destructor is going
> to be confusing feature with non-deterministic behaviour? (How would i
> know when heap allocated Socket will be destructed -> when connection
> will be closed -> if my system gonna be DoS'ed?)

The allocators (http://d-programming-language.org/class.html#allocators) 
and deallocators 
(http://d-programming-language.org/class.html#deallocators) are going 
away. The destructors 
(http://d-programming-language.org/class.html#destructors) are not.

> * Are you going to stop supporting object oriented programming? (Well,
> if *deterministic* resource managing can be only inside function, and
> not object boundaries, that's my conclusion).
>

'delete' only governs memory management. Since when deterministic 
*memory* management becomes a requirement of OOP? If you need 
deterministic resource management, you could use the dispose pattern. 
Java doesn't have a 'delete' operator either, that doesn't make Java a 
non-OOP language.

> And finally, don't get me wrong. I'm just caricaturizing my current
> worries about the language, and if I'm missing something - please hint
> me what.
>
> Sincerely,
> Mariusz Gliwiński



More information about the Digitalmars-d mailing list