Re: C++'s this() equivalent?

2023-06-15 Thread zjh via Digitalmars-d-learn
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.

Re: C++'s this() equivalent?

2023-06-15 Thread zjh via Digitalmars-d-learn
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

Re: C++'s this() equivalent?

2023-06-15 Thread Jonathan M Davis via Digitalmars-d-learn
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: > > On Friday, 16 June 2023 at 01:00:05 UTC, Steven Schveighoffer > > > > wrote: > > > B b = B.make(); // call factory function

Re: C++'s this() equivalent?

2023-06-15 Thread Jonathan M Davis via Digitalmars-d-learn
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: > > B b = B.make(); // call factory function > > > > -Steve > > Thank you for your tip. > If could simplify it a bit more, it would be even

Re: C++'s this() equivalent?

2023-06-15 Thread CM via Digitalmars-d-learn
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: B b = B.make(); // call factory function -Steve Thank you for your tip. If could simplify it a bit more, it would be even better. It's really uncomfortable without

Re: C++'s this() equivalent?

2023-06-15 Thread zjh via Digitalmars-d-learn
On Friday, 16 June 2023 at 01:00:05 UTC, Steven Schveighoffer wrote: B b = B.make(); // call factory function -Steve Thank you for your tip. If could simplify it a bit more, it would be even better. It's really uncomfortable without `this()`.

Re: C++'s this() equivalent?

2023-06-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/15/23 8:52 PM, zjh wrote: On Friday, 16 June 2023 at 00:35:48 UTC, Steven Schveighoffer wrote: Instead, you can use factory functions to initialize. How can `factory functions` be used to achieve effects similar to `'RAII'`? Thank you. B b; becomes B b = B.make(); // call factory

Re: C++'s this() equivalent?

2023-06-15 Thread zjh via Digitalmars-d-learn
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.

Re: C++'s this() equivalent?

2023-06-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/15/23 8:45 PM, zjh wrote: 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 ``` As a `C++` user, it is very terrible to simplify `RAII` without such a method. Why must `this()` be disabled?

Re: C++'s this() equivalent?

2023-06-15 Thread zjh via Digitalmars-d-learn
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 ``` -Steve 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

Re: C++'s this() equivalent?

2023-06-15 Thread zjh via Digitalmars-d-learn
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. Ali I used the `class` to call the`

Re: C++'s this() equivalent?

2023-06-15 Thread Steven Schveighoffer via Digitalmars-d-learn
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

Re: C++'s this() equivalent?

2023-06-15 Thread zjh via Digitalmars-d-learn
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){

Re: C++'s this() equivalent?

2023-06-15 Thread Ali Çehreli via Digitalmars-d-learn
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

C++'s this() equivalent?

2023-06-15 Thread zjh via Digitalmars-d-learn
```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(){

Re: C#'s 'is' equivalent in D

2019-10-10 Thread jmh530 via Digitalmars-d-learn
On Thursday, 10 October 2019 at 16:33:47 UTC, H. S. Teoh wrote: On Thu, Oct 10, 2019 at 03:58:02PM +, jmh530 via Digitalmars-d-learn wrote: On Thursday, 10 October 2019 at 15:47:58 UTC, Just Dave wrote: > In C# you can do something like: > > > if (obj is Person) > { > var

Re: C#'s 'is' equivalent in D

2019-10-10 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Oct 10, 2019 at 03:58:02PM +, jmh530 via Digitalmars-d-learn wrote: > On Thursday, 10 October 2019 at 15:47:58 UTC, Just Dave wrote: > > In C# you can do something like: > > > > > > if (obj is Person) > > { > > var person = obj as Person; > > // do stuff with

Re: C#'s 'is' equivalent in D

2019-10-10 Thread jmh530 via Digitalmars-d-learn
On Thursday, 10 October 2019 at 15:47:58 UTC, Just Dave wrote: In C# you can do something like: if (obj is Person) { var person = obj as Person; // do stuff with person... } where you can check the type of an object prior to casting. Does D have a similar

Re: C#'s 'is' equivalent in D

2019-10-10 Thread Just Dave via Digitalmars-d-learn
On Thursday, 10 October 2019 at 15:53:20 UTC, Adam D. Ruppe wrote: On Thursday, 10 October 2019 at 15:47:58 UTC, Just Dave wrote: if (obj is Person person) Looks the same as D's if(auto person = cast(Person) obj) { // use person in here } else { // it was some other type }

Re: C#'s 'is' equivalent in D

2019-10-10 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, October 10, 2019 9:47:58 AM MDT Just Dave via Digitalmars-d- learn wrote: > In C# you can do something like: > > > if (obj is Person) > { > var person = obj as Person; > // do stuff with person... > } > > where you can check the type of an object prior

Re: C#'s 'is' equivalent in D

2019-10-10 Thread drug via Digitalmars-d-learn
On 10/10/19 6:47 PM, Just Dave wrote: In C# you can do something like:     if (obj is Person)     {     var person = obj as Person;     // do stuff with person...     } where you can check the type of an object prior to casting. Does D have a similar mechanism? It's so widely

Re: C#'s 'is' equivalent in D

2019-10-10 Thread Adam D. Ruppe via Digitalmars-d-learn
On Thursday, 10 October 2019 at 15:47:58 UTC, Just Dave wrote: if (obj is Person person) Looks the same as D's if(auto person = cast(Person) obj) { // use person in here } else { // it was some other type }

Re: C#'s 'is' equivalent in D

2019-10-10 Thread Just Dave via Digitalmars-d-learn
Even though static solutions would be more performance minded, I'd actually prefer to see the runtime equivalent so I don't have to rethink how I think as performance isn't really my major concern right now.

C#'s 'is' equivalent in D

2019-10-10 Thread Just Dave via Digitalmars-d-learn
In C# you can do something like: if (obj is Person) { var person = obj as Person; // do stuff with person... } where you can check the type of an object prior to casting. Does D have a similar mechanism? It's so widely useful in the C# realm that they even added