Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

D does not offer RAII #27

Open
carun opened this issue Jun 14, 2018 · 2 comments
Open

D does not offer RAII #27

carun opened this issue Jun 14, 2018 · 2 comments

Comments

@carun
Copy link

carun commented Jun 14, 2018

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.

@timotheecour
Copy link
Owner

@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)

@PetarKirov
Copy link

@carun Counterpoint: D offers scoped classes, which can have default constructors :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants