Re: .dup vs operation on all elements
On Monday, 3 December 2018 at 20:37:22 UTC, Jonathan M Davis wrote: On Monday, December 3, 2018 1:07:24 PM MST Goksan via Digitalmars-d-learn wrote: Are there any differences between these 2 methods of copying elements? double[] array = [ 1, 20, 2, 30, 7, 11 ]; // Non dup double[6] bracket_syntax_dup = array; bracket_syntax_dup[] = array; bracket_syntax_dup[0] = 50; // Dup double[6] normal_dup = array.dup; normal_dup[0] = 100; OUTPUT: (array, bracket_syntax_dup and normal_dup respectively): [1, 20, 2, 30, 7, 11] [50, 20, 2, 30, 7, 11] [100, 20, 2, 30, 7, 11] dup allocates a new dynamic array and copies the elements of the existing dynamic array to the new one. Calling dup in order to assign to a static array is just needlessly allocating a dynamic array. The contents of the array are going to be copied to the static array regardless, but instead of just copying the elements, if you use dup, you're allocating a new dynamic array, copying the elements into that dynamic array, and then you're copying the elements into the static array. There's no point. You use dup when you want to copy the elements of a dynamic array instead of simply slicing it. Slicing gives you a new dynamic array that points to exactly the same elements. It's just copying the pointer and the length (meaning that mutating the elements of the new slice will affect the elements in the original array), whereas dup actually allocates a new block of memory for the new dynamic array to be a slice of (copying the elements over in the process), so mutating the elements in the new dynamic array then won't affect the elements in the original. Regardless, when you create a static array, it's not a slice of anything (since its elements sit directly on the stack), and when assign to it, you're simply copying the elements over. - Jonathan M Davis Then shouldn't the following output false, false, true? import std.stdio; class Programmer { bool is_confused = false; void setConfusion(bool confusion_status) { is_confused = confusion_status; } } void main() { Programmer[6] array = new Programmer(); Programmer[6] bracket_syntax_dup = array; bracket_syntax_dup[] = array; Programmer[6] normal_dup = array.dup; normal_dup[0].setConfusion(true); bracket_syntax_dup[0] = new Programmer(); writeln(array[0].is_confused); writeln(bracket_syntax_dup[0].is_confused); writeln(normal_dup[0].is_confused); }
Am I misusing with?
This seems to work fine file = File("test.txt", "r"); with (file) { scope(exit) close(); foreach (string line; file.lines()) { line_array ~= line; } } however: file = File("test.txt", "r"); with (file) { scope(exit) close(); foreach (string line; lines()) { line_array ~= line; } } Tells me I'm attempting to read from an unopened file, what's going on here? It seems like I'm able to use lines() like this within with statements unless they're in my foreach iterator. Is this a bug or intended behaviour?
Re: Am I misusing with?
On Saturday, 19 January 2019 at 20:07:34 UTC, Rubn wrote: On Saturday, 19 January 2019 at 17:49:31 UTC, faissaloo wrote: [...] If you look at the implementation, "lines" is a struct. https://github.com/dlang/phobos/blob/v2.084.0/std/stdio.d#L4330 [...] Ah that makes some sense, thanks for the explanation.
Modulo that 'wraps' the number?
In Python -1%3 == 2 however in D -1%3 == -1 Is there a standard library function or something that gives me the Python version of modulo?
Does new X() return a pointer or not?
I have the following function static Component* constructComponent(int value) { return (new ComponentChild(value)); } ComponentChild is a derived class of Component. However, I get told that ComponentChild cannot be converted to Component*. I'm confused here, does new return a heap pointer or not? Are objects automatically assumed to be pointers?
Re: Does new X() return a pointer or not?
Thanks alot everyone for your replies, this makes sense now.
getOverloads trait doesn't work on functions
I'm trying to use: ``` __traits(getOverloads, fn) ``` But I get the error expected 2 arguments for getOverloads but had 1 Is there an alternative I can use?
Mixin can't access library symbols?
How can I get a mixin to implicitly include the symbols from its surrounding context? Is this possible?
Re: Mixin can't access library symbols?
On Friday, 3 May 2019 at 17:51:39 UTC, Adam D. Ruppe wrote: On Friday, 3 May 2019 at 17:48:50 UTC, faissaloo wrote: How can I get a mixin to implicitly include the symbols from its surrounding context? Is this possible? What's your big picture goal? Do you have sample code you have tried so far? Also, are you talking mixin("string") or mixin template? They work differently with symbols too. My sample code is too big to be useful. The use of my mixin looks like: mixin(defState!("XEvent value;")); But it tells me it can't find the symbol XEvent despite me having used it elsewhere.
alias this and struct allocation
I've been having some memory issues (referenced objects turning to nulls for no apparent reason) and I was wondering if I've misunderstood how allocation works when instantiating a struct that uses alias this: import std.stdio; struct Parent { int a; } struct Child { Parent base; alias base this; int y; } auto myStructMaker() { return new Child(Parent(10),20); } void main() { writeln(*myStructMaker()); } In this example is the data in base guaranteed to exist? Or is base definitely part of the allocation of Child on the heap?
Re: alias this and struct allocation
On Monday, 6 May 2019 at 15:17:37 UTC, aliak wrote: Do you have an example of a referenced object turning to null? We may be able to spot something Unfortunately I haven't managed to produce an example any smaller than my entire codebase
In what situation can new Struct() return null?
My program contains the following statement: auto newChildNode = new Node(); In debugging I have found that this pointer evaluates to null, what could cause this? I should have plenty of memory, my only other idea is some sort of heap corruption.
Re: In what situation can new Struct() return null?
On Friday, 10 May 2019 at 12:19:29 UTC, Cym13 wrote: On Friday, 10 May 2019 at 10:11:51 UTC, faissaloo wrote: My program contains the following statement: auto newChildNode = new Node(); In debugging I have found that this pointer evaluates to null, what could cause this? I should have plenty of memory, my only other idea is some sort of heap corruption. Could you share a complete, concise, compilable example demonstrating that bug? One line is rather short to understand what's happening. Unfortunately not, I can't seem to reproduce it outside my codebase.
Re: In what situation can new Struct() return null?
On Friday, 10 May 2019 at 17:54:44 UTC, H. S. Teoh wrote: Perhaps try Dustmite on it? AFAIK, calling new on a struct should never return null. So there must be something else not quite right here. But without actual code it's anybody's guess as to what it might be. The last time I heard somebody run into this, it was caused by some unrelated compiler codegen bug that left the CPU registers in an inconsistent state, thus causing an unrelated call to `new` to return the wrong value. Perhaps you could try a different compiler to see if that makes a difference? T I've tried a number of different compilers and the issue persists, I think the issue is the garbage collector. This issue did have spontaneously disappear but generally with my project there seem to be a number of memory corruption issues that as far as I can tell, shouldn't be happening. I've submitted a bug report here: https://issues.dlang.org/show_bug.cgi?id=19860