digitalmars.D.learn - C++'s this() equivalent?
- zjh (42/42) Jun 15 2023 ```d
- =?UTF-8?Q?Ali_=c3=87ehreli?= (11/17) Jun 15 2023 I have difficulty understanding the question. I think the nested
- zjh (29/33) Jun 15 2023 ```d
- Steven Schveighoffer (9/12) Jun 15 2023 You cannot have parameter-less constructors in D structs. However,
- zjh (6/11) Jun 15 2023 As a `C++` user, it is very terrible to simplify `RAII` without
- Steven Schveighoffer (10/24) Jun 15 2023 D was not originally designed with RAII. That was added later (D1
- zjh (6/7) Jun 15 2023 -Steve
- Steven Schveighoffer (7/15) Jun 15 2023 B b;
- zjh (5/7) Jun 15 2023 Thank you for your tip.
- CM (18/26) Jun 15 2023 One could define a static opCall in his aggregate. It's still a
- zjh (3/9) Jun 15 2023 Thank you for your reply. This should be enough, as this type of
- Jonathan M Davis (49/57) Jun 15 2023 The reasons for it have to do with how D in general is set up to require
- zjh (3/7) Jun 15 2023 Thank you for your `wonderful summary`. It's worth saving the
- Jonathan M Davis (5/18) Jun 15 2023 This should say that it's set up to require that the _default_ value of ...
- zjh (11/15) Jun 15 2023 I used the `class` to call the` constructor/destructor
```d import core.stdc.stdio; struct A{ static int e=40; struct B{ int m; this(int i){ printf("%i",e);m=i; } ~this(){ e=m; printf("%i",e); } } void f(){ B b=e;e=6; } void g(){ printf("%i",e); } } extern(C)void main(){ A a; a.f(); a.g(); } ``` I want `B` to become the following version similar to 'C++'. What should I do? ```cpp struct B{ int m; B(){ printf("%i",e);m=e; } ~B(){ e=m; printf("%i",e); } } ``` Thank you.
Jun 15 2023
On 6/15/23 08:15, zjh wrote:I want `B` to become the following version similar to 'C++'. What should I do?I have difficulty understanding the question. I think the nested structs, extern(C), static members, etc. confuse me. I am assuming they are not related to this question.```cpp struct B{ int m; B(){Do you want to be able to use the name of the user-defined type to mean "constructor", "destructor", etc? If so, no, it's not possible without preprocessing the text potentially with a C preprocessor. And this is by design: In D, we rename the type and be done with it. In C++, one needs to rename a number of functions as well. Ali
Jun 15 2023
On Thursday, 15 June 2023 at 21:48:10 UTC, Ali Çehreli wrote:And this is by design: In D, we rename the type and be done with it. In C++, one needs to rename a number of functions as well. Ali```d static int e=40; struct B{ int m; this(int i){ printf("%i",e);m=i; } ~this(){ e=m; printf("%i",e); } //After leaving the scope, restore the original value } // definition, // usage void f(){ B b=e;e=6; }//e Restore to 40. ``` But I want like follow, but due to the `disable this()`,I can't: ```d void f(){ B b;e=6; } ``` Because disabled `this()`, the behavior of `this(int i)`is inconsistent and cannot achieve similar `RAII` as `C++`. What should I do?
Jun 15 2023
On 6/15/23 8:31 PM, zjh wrote:Because disabled `this()`, the behavior of `this(int i)`is inconsistent and cannot achieve similar `RAII` as `C++`. What should I do?You cannot have parameter-less constructors in D structs. However, destruction works the same. Instead, you can use factory functions to initialize. But there is no way in D to have e.g.: ```d B b; // runs a constructor ``` -Steve
Jun 15 2023
On Friday, 16 June 2023 at 00:35:48 UTC, Steven Schveighoffer wrote:But there is no way in D to have e.g.: ```d B b; // runs a constructor ``` -SteveAs a `C++` user, it is very terrible to simplify `RAII` without such a method. Why must `this()` be disabled? Can't there be a constructor? In a constructor, you can complete `some work`. Without it, how can you simplify `RAII`?.
Jun 15 2023
On 6/15/23 8:45 PM, zjh wrote:On Friday, 16 June 2023 at 00:35:48 UTC, Steven Schveighoffer wrote:D was not originally designed with RAII. That was added later (D1 structs did not have ctors or dtors). The benefit of *not* having parameterless constructors is that default construction is always defined *and* available at compile time. What is wrong with using a parameter constructor or factory function? What is the feature you are trying to build that you can with C++, but not with D? I'm not saying I agree with the limitation, just want to know more. -SteveBut there is no way in D to have e.g.: ```d B b; // runs a constructor ```As a `C++` user, it is very terrible to simplify `RAII` without such a method. Why must `this()` be disabled? Can't there be a constructor? In a constructor, you can complete `some work`. Without it, how can you simplify `RAII`?.
Jun 15 2023
On Friday, 16 June 2023 at 00:35:48 UTC, Steven Schveighoffer wrote:Instead, you can use factory functions to initialize.-Steve How can `factory functions` be used to achieve effects similar to `'RAII'`? Thank you.
Jun 15 2023
On 6/15/23 8:52 PM, zjh wrote:On Friday, 16 June 2023 at 00:35:48 UTC, Steven Schveighoffer wrote:B b; becomes B b = B.make(); // call factory function All my uses of RAII depend on copy construction and destruction. Construction is easy enough because I have to explicitly declare it anyway. -SteveInstead, you can use factory functions to initialize.How can `factory functions` be used to achieve effects similar to `'RAII'`? Thank you.
Jun 15 2023
On Friday, 16 June 2023 at 01:00:05 UTC, Steven Schveighoffer wrote:B b = B.make(); // call factory function -SteveThank you for your tip. If could simplify it a bit more, it would be even better. It's really uncomfortable without `this()`.
Jun 15 2023
On Friday, 16 June 2023 at 01:18:25 UTC, zjh wrote:On Friday, 16 June 2023 at 01:00:05 UTC, Steven Schveighoffer wrote:One could define a static opCall in his aggregate. It's still a factory function, but one might prefer the syntax a bit more. ```d immutable defaultName = "John"; struct Man { static auto opCall() { typeof(this) this_; this_.name = defaultName; return this_; } string name; } auto m = Man(); assert(m.name == defaultName); ``` Do note that one cannot simultaneously have a constructor and a static opCall, even if their parameters differ.B b = B.make(); // call factory function -SteveThank you for your tip. If could simplify it a bit more, it would be even better. It's really uncomfortable without `this()`.
Jun 15 2023
On Friday, 16 June 2023 at 01:45:35 UTC, CM wrote:```d auto m = Man(); assert(m.name == defaultName); ``` Do note that one cannot simultaneously have a constructor and a static opCall, even if their parameters differ.Thank you for your reply. This should be enough, as this type of RAII is basically a `temporary struct`.
Jun 15 2023
On Thursday, June 15, 2023 7:18:25 PM MDT zjh via Digitalmars-d-learn wrote:On Friday, 16 June 2023 at 01:00:05 UTC, Steven Schveighoffer wrote:The reasons for it have to do with how D in general is set up to require that the value of all types be known at compile-time. Types in general use default initialization rather than default construction. The language then depends on this for a variety of things. For instance, when you construct a an array (dynamic or static), it fills in all of the values in the array by bit-blitting the init value of the element type onto the elements. Similarly, when you expand the length of a dynamic array, it bit-blits the init value of the element type onto the new elements. Other language features such as in and out on function parameters rely on the init value as well. For all of that to work, the type's init value must be known at compile-time, which means that default construction is off the table. For most things, this is just fine and is quite beneficial. However, it does make certain things (like RAII) more annoying than they are in C++. That being said, as has been pointed out, the simple workaround if you want RAII is to use a factory function. It's a bit of extra typing, since you have to call a function instead of simply declaring the variable, but the factory function then works like the constructor would have in C++, and the destructor does the clean-up like it would have in C++. It just comes with the downsides of the extra typing and the fact that it's possible to have variables of that type that did not go through the factory function, because they were default-initialized with the init value rather than constructed via the factory function. To work around that, you can disable this so that the type doesn't have an init value, but then it can't be used in any context where the init value would be required, which can become very annoying. Unlike structs, classes can have default constructors, but that's because they sit on the heap, and technically, when you refer to a class type, you're almost always referring to a class reference rather than to the object itself. And the class reference has an init value of null, so it can be used in all of the places where an init value is required, whereas a class object is never actually used in such a situation. For RAII, as an alternative to using a factory function on a struct, you can use a class and then use https://dlang.org/phobos/std_typecons.html#scoped to ensure that it gets destroyed when it leaves scope. There are also scope statements for ensuring that something is run when you exit the scope, but it isn't really RAII, since you'd still have to call the initialization portion of things separately. E.G. doSomething(); scope(exit) undoSomething(); It just makes it easier to ensure that what you want to run when the scope is exited is always run. So, there are several ways that you could go about trying to get behavior similar to RAII in C++ even if you can't actually have RAII. Probably the best method though is to just use a factory function on a struct. Yes, it's more annoying than proper RAII, but it's a side effect of other benefits that D gives that C++ does not. - Jonathan M DavisB b = B.make(); // call factory function -SteveThank you for your tip. If could simplify it a bit more, it would be even better. It's really uncomfortable without `this()`.
Jun 15 2023
On Friday, 16 June 2023 at 01:54:22 UTC, Jonathan M Davis wrote:```d doSomething(); scope(exit) undoSomething(); ```Thank you for your `wonderful summary`. It's worth saving the link.
Jun 15 2023
On Thursday, June 15, 2023 7:54:22 PM MDT Jonathan M Davis via Digitalmars-d- learn wrote:On Thursday, June 15, 2023 7:18:25 PM MDT zjh via Digitalmars-d-learn wrote:This should say that it's set up to require that the _default_ value of all types be known at compile-time. - Jonathan M DavisOn Friday, 16 June 2023 at 01:00:05 UTC, Steven Schveighoffer wrote:The reasons for it have to do with how D in general is set up to require that the value of all types be known at compile-time.B b = B.make(); // call factory function -SteveThank you for your tip. If could simplify it a bit more, it would be even better. It's really uncomfortable without `this()`.
Jun 15 2023
On Thursday, 15 June 2023 at 21:48:10 UTC, Ali Çehreli wrote:I have difficulty understanding the question. I think the nested structs, extern(C), static members, etc. confuse me. I am assuming they are not related to this question.AliI used the `class` to call the` constructor/destructor (destroy)`, but after they leave the scope, they do not call the`destructor (destroy)`. `Nested class` is because I want to directly use members of `external struct`, but in reality, I cannot access them directly. Therefore, I only have to use ` static members`,. `extern(C)` is to use `BetterC`,. The purpose is mainly to complete `RAII`, completing one part of the work after entering the scope and another part of the work after leaving the scope.
Jun 15 2023