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: Dynamic length string array at compile time?

2023-06-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/15/23 9:59 PM, Mark Fisher wrote: Hi, So what if I wanted to build the strings within my library but have the client append their own data, for example if I want to have declarative extensions? Could this be done by using a mixin template to create the strings in the client? Well, as

Re: Dynamic length string array at compile time?

2023-06-15 Thread Mark Fisher via Digitalmars-d-learn
On Wednesday, 7 June 2023 at 00:18:54 UTC, Steven Schveighoffer wrote: So for instance, you can do: ```d immutable string[] strs = mod1.makeStrings() ~ mod2.makeStrings() ~ ...; ``` In a centralized place. -Steve Hi, So what if I wanted to build the strings within my library but have

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

Para que sirve o que son las variables "immutable"?

2023-06-15 Thread Danico via Digitalmars-d-learn
Tengo una duda y es que en la documentacion de std.json hay un ejemplo de codigo que por identidicador ponen "immutable", nose para que sirve o de que trata. :( alguien me diga porfis :)

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

Re: Calling C functions that modify a string

2023-06-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/15/23 2:21 PM, bachmeier wrote: On Thursday, 15 June 2023 at 15:53:57 UTC, Steven Schveighoffer wrote: On 6/15/23 10:04 AM, Jonathan M Davis wrote: On Thursday, June 15, 2023 7:18:06 AM MDT Steven Schveighoffer via Digitalmars-d-learn wrote: But in general, if you want a mutable

Re: Calling C functions that modify a string

2023-06-15 Thread bachmeier via Digitalmars-d-learn
On Thursday, 15 June 2023 at 15:53:57 UTC, Steven Schveighoffer wrote: On 6/15/23 10:04 AM, Jonathan M Davis wrote: On Thursday, June 15, 2023 7:18:06 AM MDT Steven Schveighoffer via Digitalmars-d-learn wrote: But in general, if you want a mutable character array that's zero terminated, you

Re: Calling C functions that modify a string

2023-06-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/15/23 10:04 AM, Jonathan M Davis wrote: On Thursday, June 15, 2023 7:18:06 AM MDT Steven Schveighoffer via Digitalmars-d-learn wrote: But in general, if you want a mutable character array that's zero terminated, you need to make a copy with a zero terminator, but type it as mutable. I'm

Re: Parser

2023-06-15 Thread Ben Jones via Digitalmars-d-learn
On Wednesday, 14 June 2023 at 09:28:57 UTC, Cecil Ward wrote: I’m thinking that I might had to end up writing a partial, rather rough parser for parts of the D language. Could I get some suggestions for help that I might find in the way of software components? D has a very powerful regex

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: Calling C functions that modify a string

2023-06-15 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, June 15, 2023 7:18:06 AM MDT Steven Schveighoffer via Digitalmars-d-learn wrote: > But in general, if you want a mutable character array that's zero > terminated, you need to make a copy with a zero terminator, but type it > as mutable. I'm surprised there isn't a way to do this

Re: Calling C functions that modify a string

2023-06-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/15/23 9:18 AM, Steven Schveighoffer wrote: So interestingly enough, toStringz is pure, and returns an unrelated type, so you shouldn't need to cast. However, for some reason, it does require a cast. That seems like a bug to me. Oh wait, a pure function can return immutable data that

Re: Calling C functions that modify a string

2023-06-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/14/23 11:29 PM, Pat Maddox wrote: Hi there, I want to call a C function that upcases a string. I have something working, I just want to check in here to see if there's a better approach that I'm missing. I ask because `std.string.toStringZ()` returns an `immutable char *`. As far as I