D does not offer RAII
carun opened this issue · 2 comments
D offers only scoped destruction and not resource acquisition is initialization. Basically the resource is not always acquired during initialization as struct
in D can't have default constructors. Library implementations like Automem makes using RAII in D as easy as C++, but only for heap memory and not for all kinds of resources (like mutex, etc). It is unfair to say that D supports RAII. Its RAII is nowhere close to what C++ offers that it shouldn't be called RAII by definition.
@carun
added a caveat in e062744
Adding a dummy argument is the common workaround for lack of default struct constructors:
struct A{
T data;
this(int dummy){
// initialize data
}
~this(){
// deinitialize data
}
}
void main(){
void fun(){
auto a = A(0);
// ~A called deterministically upon scope exit
}
}
What are the limitations of this workaround pattern? (honest question)
@carun Counterpoint: D offers scoped classes, which can have default constructors :P