Re: Drawbacks of exceptions being globally allocated

2021-08-15 Thread Mathias LANG via Digitalmars-d-learn
On Monday, 16 August 2021 at 05:36:07 UTC, Tejas wrote: That is why I was using heapAllocate, because using `new` on scope allocates exception on stack, and if you exit, then the exception is basically freed before it even gets caught. It fails even when you catch within the same function.

Re: Drawbacks of exceptions being globally allocated

2021-08-15 Thread Tejas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 20:23:03 UTC, Paul Backus wrote: On Sunday, 15 August 2021 at 18:47:27 UTC, Tejas wrote: Do you see anything wrong with the following `emplace`-allocated, RAII following exceptions: [...] Is this good enough for general use now? Any other drawbacks? It only

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread Tejas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 20:41:51 UTC, james.p.leblanc wrote: Greetings, I have been trying to get a working example of slice assignment operator overloading ... and am befuddled. From the spec (section 20.6.2), the code below appears: struct A { int opIndexAssign(int

Re: Drawbacks of exceptions being globally allocated

2021-08-15 Thread Paul Backus via Digitalmars-d-learn
On Monday, 16 August 2021 at 02:26:04 UTC, Tejas wrote: Agh >_< if I remove the `scope`and replace it with `auto`? No longer having anything to do with the stack or RAII, just using malloc + emplace instead of GC? Yeah it might leak memory unless the catch block explicitly frees the

Re: Drawbacks of exceptions being globally allocated

2021-08-15 Thread Tejas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 20:23:03 UTC, Paul Backus wrote: On Sunday, 15 August 2021 at 18:47:27 UTC, Tejas wrote: Do you see anything wrong with the following `emplace`-allocated, RAII following exceptions: [...] Is this good enough for general use now? Any other drawbacks? It only

Re: Anyway to achieve the following

2021-08-15 Thread Carl Sturtivant via Digitalmars-d-learn
On Sunday, 15 August 2021 at 07:10:17 UTC, JG wrote: Hi, This is exactly the behaviour I was trying to obtain. It however comes with a fair amount of overhead, as can be seen in the following llvm ir: [...] What you are asking for are reference variables. C++ has them: the example here

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread james.p.leblanc via Digitalmars-d-learn
On Sunday, 15 August 2021 at 21:28:53 UTC, james.p.leblanc wrote: On Sunday, 15 August 2021 at 21:15:02 UTC, Bastiaan Veelo wrote: On Sunday, 15 August 2021 at 20:41:51 UTC, james.p.leblanc — Bastiaan. Bastiaan, Thanks once again, James On Sunday, 15 August 2021 at 21:28:53 UTC,

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread james.p.leblanc via Digitalmars-d-learn
On Sunday, 15 August 2021 at 21:15:02 UTC, Bastiaan Veelo wrote: On Sunday, 15 August 2021 at 20:41:51 UTC, james.p.leblanc — Bastiaan. Bastiaan, Thanks kindly for your response! Unfortunately, I do not see what the program does. I mean A is a structure that has only functions. So, how

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread russhy via Digitalmars-d-learn
There is an example here: http://www.rosettacode.org/wiki/Multi-dimensional_array#D Look at the Matrix struct

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 15 August 2021 at 20:41:51 UTC, james.p.leblanc wrote: I have been trying to get a working example of slice assignment operator overloading ... and am befuddled. From the spec (section 20.6.2), the code below appears: struct A { int opIndexAssign(int v); //

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread Bastiaan Veelo via Digitalmars-d-learn
On Sunday, 15 August 2021 at 20:41:51 UTC, james.p.leblanc wrote: struct A { int opIndexAssign(int v); // overloads a[] = v int opIndexAssign(int v, size_t[2] x); // overloads a[i .. j] = v int[2] opSlice(size_t x, size_t y); // overloads i .. j }

Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread james.p.leblanc via Digitalmars-d-learn
Greetings, I have been trying to get a working example of slice assignment operator overloading ... and am befuddled. From the spec (section 20.6.2), the code below appears: struct A { int opIndexAssign(int v); // overloads a[] = v int opIndexAssign(int v, size_t[2]

Re: Drawbacks of exceptions being globally allocated

2021-08-15 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 15 August 2021 at 18:47:27 UTC, Tejas wrote: Do you see anything wrong with the following `emplace`-allocated, RAII following exceptions: [...] Is this good enough for general use now? Any other drawbacks? It only works if you're throwing and catching in the same function.

Re: Drawbacks of exceptions being globally allocated

2021-08-15 Thread Tejas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 16:23:25 UTC, Ali Çehreli wrote: On 8/15/21 2:10 AM, Alexandru Ermicioi wrote: >> This may be useful in some cases but in general, these colatteral >> exceptions don't carry much information and I don't think anybody >> looks at them. Usually, the first one is the

Re: partial initialization of fixed size ("static") arrays

2021-08-15 Thread kdevel via Digitalmars-d-learn
On Saturday, 14 August 2021 at 23:09:14 UTC, Paul Backus wrote: On Saturday, 14 August 2021 at 14:04:47 UTC, kdevel wrote: ~~~ char [7] d7 = "x"; // okay string s = "x"; char [7] c7 = s; // throws RangeError ~~~ What justifies that the compiler behaves differently on two terms ('s',

Re: Drawbacks of exceptions being globally allocated

2021-08-15 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Sunday, 15 August 2021 at 16:23:25 UTC, Ali Çehreli wrote: That output contains two automatically chained exceptions: Failed: Main failed This failed too: The destructor failed Ali Hmm, wasn't aware of such use case (results of too much java :)). Considering this case I'd say it is

Re: Anyway to achieve the following

2021-08-15 Thread Johan via Digitalmars-d-learn
On Sunday, 15 August 2021 at 16:49:22 UTC, Paul Backus wrote: On Sunday, 15 August 2021 at 07:10:17 UTC, JG wrote: Hi, This is exactly the behaviour I was trying to obtain. It however comes with a fair amount of overhead, as can be seen in the following llvm ir: [...] I'm not really

Re: Anyway to achieve the following

2021-08-15 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 15 August 2021 at 07:10:17 UTC, JG wrote: Hi, This is exactly the behaviour I was trying to obtain. It however comes with a fair amount of overhead, as can be seen in the following llvm ir: [...] I'm not really familiar with llvm ir, but looking at it on godbolt, it seems

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/15/21 2:10 AM, rempas wrote: So when I'm doing something like the following: `string name = "John";` Then what's the actual type of the literal `"John"`? In the chapter [Calling C functions](https://dlang.org/spec/interfaceToC.html#calling_c_functions) in the "Interfacing with C" page,

Re: Drawbacks of exceptions being globally allocated

2021-08-15 Thread Ali Çehreli via Digitalmars-d-learn
On 8/15/21 2:10 AM, Alexandru Ermicioi wrote: >> This may be useful in some cases but in general, these colatteral >> exceptions don't carry much information and I don't think anybody >> looks at them. Usually, the first one is the one that explains the >> error case. > That is just an

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread Ali Çehreli via Digitalmars-d-learn
Lot's of great information and pointers already. I will try from another angle. :) On 8/14/21 11:10 PM, rempas wrote: > So when I'm doing something like the following: `string name = "John";` > Then what's the actual type of the literal `"John"`? As you say and as the code shows, there are

Re: how to import .lib library

2021-08-15 Thread Marcone via Digitalmars-d-learn
On Sunday, 15 August 2021 at 10:12:17 UTC, Timofeyka wrote: Thank you for your reply! I wanted to link to my project another project without source code. This tutorial can help you create yours libs: https://wiki.dlang.org/Win32_DLLs_in_D

Re: how to import .lib library

2021-08-15 Thread Marcone via Digitalmars-d-learn
On Sunday, 15 August 2021 at 09:49:39 UTC, Timofeyka wrote: Hello! I may have a very stupid question, but still. How do I include a .lib library? How to use it in your code? Inside the source code you can use pragma. Example: pragma(lib, "gdi32.lib"); In DMD command line you can use -L flag

Re: how to import .lib library

2021-08-15 Thread Mike Parker via Digitalmars-d-learn
On Sunday, 15 August 2021 at 10:40:36 UTC, jfondren wrote: Yeah, that's not possible. You either need the source or a set of D interface files that declares all the symbols you need. Meaning, it is possible. On Windows where I assume these .lib files are: I mentioned C libraries in an

Re: how to import .lib library

2021-08-15 Thread jfondren via Digitalmars-d-learn
On Sunday, 15 August 2021 at 10:19:33 UTC, Mike Parker wrote: On Sunday, 15 August 2021 at 10:12:17 UTC, Timofeyka wrote: Thank you for your reply! I wanted to link to my project another project without source code. Yeah, that's not possible. You either need the source or a set of D

Re: Nondeterministic unittest debugging problem.

2021-08-15 Thread Rekel via Digitalmars-d-learn
Note you might need to open the screenshots externally, as they are cut off by the forum.

Nondeterministic unittest debugging problem.

2021-08-15 Thread Rekel via Digitalmars-d-learn
I am unsure where to mention this as I am utterly lost as to what the issue is, which I can simply best describe with 2 screenshots. Though I'll first give a short description. For some reason my debugger is "\" for one of my variables in one of my unittests. This is however not the case

Re: how to import .lib library

2021-08-15 Thread Mike Parker via Digitalmars-d-learn
On Sunday, 15 August 2021 at 10:12:17 UTC, Timofeyka wrote: Thank you for your reply! I wanted to link to my project another project without source code. Yeah, that's not possible. You either need the source or a set of D interface files that declares all the symbols you need. The compiler

Re: how to import .lib library

2021-08-15 Thread Timofeyka via Digitalmars-d-learn
On Sunday, 15 August 2021 at 10:05:15 UTC, Mike Parker wrote: You don't import a .lib file. They are for the linker, not the compiler. How you make use of it depends on what sort of library it is and how you're building your project. If this is all new to you, it will be easier just to

Re: how to import .lib library

2021-08-15 Thread Mike Parker via Digitalmars-d-learn
On Sunday, 15 August 2021 at 09:49:39 UTC, Timofeyka wrote: Hello! I may have a very stupid question, but still. How do I include a .lib library? How to use it in your code? You don't import a .lib file. They are for the linker, not the compiler. How you make use of it depends on what sort of

how to import .lib library

2021-08-15 Thread Timofeyka via Digitalmars-d-learn
Hello! I may have a very stupid question, but still. How do I include a .lib library? How to use it in your code?

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread rempas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 09:06:14 UTC, Mike Parker wrote: The D `string` is an alias for `immutable(char)[]`, immutable contents of a mutable array reference (`immutable(char[])` would mean the array reference is also immutable). You don't want to assign that to a `char*`, because then

Re: Drawbacks of exceptions being globally allocated

2021-08-15 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Sunday, 15 August 2021 at 00:15:32 UTC, Ali Çehreli wrote: Even though this feature is probably never used, in D, multiple exception objects are chained. For example, you can throw e.g. in a destructor when there is an active exception in flight and that second object gets attached to the

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread Mike Parker via Digitalmars-d-learn
On Sunday, 15 August 2021 at 08:11:39 UTC, rempas wrote: I mean that in C, we can assign a string literal into a `char*` and also a `const char*` type without getting a compilation error while in D, we can only assign it to a `const char*` type. I suppose that's because of C doing explicit

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread rempas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 09:01:17 UTC, jfondren wrote: They don't do the same thing. toStringz always copies, always GC-allocates, and always NUL-terminates. `cast(char*)` only does what you want in the case that you're applying it a string literal. But in that case you shouldn't cast, you

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread jfondren via Digitalmars-d-learn
On Sunday, 15 August 2021 at 08:56:07 UTC, rempas wrote: On Sunday, 15 August 2021 at 08:53:50 UTC, Tejas wrote: External C libraries expect strings to be null terminated, so if you do use `.dup`, use `.toStringz` as well. Yeah, yeah I got that. My question is, if I should avoid

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread rempas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 08:53:50 UTC, Tejas wrote: External C libraries expect strings to be null terminated, so if you do use `.dup`, use `.toStringz` as well. Yeah, yeah I got that. My question is, if I should avoid `cast(char*)` and use `.toStringz` while both do the exact same

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread Tejas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 08:51:19 UTC, rempas wrote: On Sunday, 15 August 2021 at 08:47:39 UTC, jfondren wrote: dup() isn't aware of the NUL since that's outside the slice of the string. It only copies the chars in "John". You can use toStringz to ensure NUL termination:

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread rempas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 08:47:39 UTC, jfondren wrote: dup() isn't aware of the NUL since that's outside the slice of the string. It only copies the chars in "John". You can use toStringz to ensure NUL termination: https://dlang.org/phobos/std_string.html#.toStringz Is there something

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread jfondren via Digitalmars-d-learn
On Sunday, 15 August 2021 at 08:11:39 UTC, rempas wrote: On Sunday, 15 August 2021 at 07:43:59 UTC, jfondren wrote: ```d unittest { char* s = "John".dup.ptr; s[0] = 'X'; // no segfaults assert(s[0..4] == "Xohn"); // ok } ``` Well, that one didn't worked out really well for me.

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread jfondren via Digitalmars-d-learn
On Sunday, 15 August 2021 at 07:47:27 UTC, jfondren wrote: On Sunday, 15 August 2021 at 07:43:59 UTC, jfondren wrote: On Sunday, 15 August 2021 at 06:10:53 UTC, rempas wrote: ```d unittest { char* s = "John".dup.ptr; s[0] = 'X'; // no segfaults assert(s[0..4] == "Xohn"); // ok } ```

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread rempas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 08:17:47 UTC, rikki cattermole wrote: pragma is a set of commands to the compiler that may be compiler specific. In the case of the msg command, it tells the compiler to output a message to stdout during compilation. Thanks man!

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread rikki cattermole via Digitalmars-d-learn
On 15/08/2021 8:11 PM, rempas wrote: Still don't know what "pragma" does but thank you. pragma is a set of commands to the compiler that may be compiler specific. In the case of the msg command, it tells the compiler to output a message to stdout during compilation.

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread rempas via Digitalmars-d-learn
On Sunday, 15 August 2021 at 07:43:59 UTC, jfondren wrote: ```d unittest { pragma(msg, typeof("John")); // string pragma(msg, is(typeof("John") == immutable(char)[])); // true } ``` Still don't know what "pragma" does but thank you. ```d void zerort(string s) {

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread jfondren via Digitalmars-d-learn
On Sunday, 15 August 2021 at 07:43:59 UTC, jfondren wrote: On Sunday, 15 August 2021 at 06:10:53 UTC, rempas wrote: ```d unittest { char* s = "John".dup.ptr; s[0] = 'X'; // no segfaults assert(s[0..4] == "Xohn"); // ok } ``` So am I going to have an extra runtime cost having to

Re: What exactly are the String literrals in D and how they work?

2021-08-15 Thread jfondren via Digitalmars-d-learn
On Sunday, 15 August 2021 at 06:10:53 UTC, rempas wrote: So when I'm doing something like the following: `string name = "John";` Then what's the actual type of the literal `"John"`? ```d unittest { pragma(msg, typeof("John")); // string pragma(msg, is(typeof("John") ==

Re: Anyway to achieve the following

2021-08-15 Thread JG via Digitalmars-d-learn
On Saturday, 14 August 2021 at 20:50:47 UTC, Carl Sturtivant wrote: ``` struct S { int x = 1234; } void main() { import std.stdio; S s; //construction of a using &(s.x) auto a = Ref!(int)(); writeln(a); //displays 1234 s.x += 1; writeln(a); //displays 1235 a += 1;

What exactly are the String literrals in D and how they work?

2021-08-15 Thread rempas via Digitalmars-d-learn
So when I'm doing something like the following: `string name = "John";` Then what's the actual type of the literal `"John"`? In the chapter [Calling C functions](https://dlang.org/spec/interfaceToC.html#calling_c_functions) in the "Interfacing with C" page, the following is said: Strings are