Re: Constructing arrays of structs

2024-01-23 Thread Danilo via Digitalmars-d-learn
On Tuesday, 23 January 2024 at 18:15:29 UTC, Stephen Tashiro wrote: If the constructor of a class needs to create an array of structs whose dimensions are inputs, what's the syntax for doing this? For a non-example, the following program errors in main() because in t.array[][] "index [0] is

Re: Constructing arrays of structs

2024-01-23 Thread Renato via Digitalmars-d-learn
On Tuesday, 23 January 2024 at 19:32:31 UTC, Stephen Tashiro wrote: Thank you. I don't really understand what the syntax new Point[][](the_dimension,the_dimension); denotes. Does it represent a function? To look up this topic, what are the proper keywords? By experimentation, I found

Re: Constructing arrays of structs

2024-01-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, January 23, 2024 12:32:31 PM MST Stephen Tashiro via Digitalmars- d-learn wrote: > On Tuesday, 23 January 2024 at 18:23:22 UTC, Renato wrote: > > This works , your mistake was to not actually assign the array > > to the class' field! > > > > Change this line: > > > > ```d > > auto

Re: Constructing arrays of structs

2024-01-23 Thread Stephen Tashiro via Digitalmars-d-learn
On Tuesday, 23 January 2024 at 18:23:22 UTC, Renato wrote: This works , your mistake was to not actually assign the array to the class' field! Change this line: ```d auto array = new Point[][](the_dimension,the_dimension); ``` To this: ```d this.array = new

Re: Constructing arrays of structs

2024-01-23 Thread Renato via Digitalmars-d-learn
On Tuesday, 23 January 2024 at 18:15:29 UTC, Stephen Tashiro wrote: If the constructor of a class needs to create an array of structs whose dimensions are inputs, what's the syntax for doing this? For a non-example, the following program errors in main() because in t.array[][] "index [0] is

Constructing arrays of structs

2024-01-23 Thread Stephen Tashiro via Digitalmars-d-learn
If the constructor of a class needs to create an array of structs whose dimensions are inputs, what's the syntax for doing this? For a non-example, the following program errors in main() because in t.array[][] "index [0] is out of bounds". import std.stdio; struct Point { uint

Re: Why is it difficult to reference arrays in structs?

2019-10-02 Thread Mike Parker via Digitalmars-d-learn
On Thursday, 3 October 2019 at 04:32:52 UTC, Brett wrote: Ok, fine! auto r = Now r is a reference, great! No, r is a pointer, not a reference. D does not have reference variables. But now the semantics of using the array completely change and errors abound! Probably because you're

Re: Why is it difficult to reference arrays in structs?

2019-10-02 Thread mipri via Digitalmars-d-learn
On Thursday, 3 October 2019 at 04:32:52 UTC, Brett wrote: struct Y { double q; } struct X { Y[] a; } X x; auto r = x.a; r is not a reference?!?! Arrays are (ptr,length) pairs. r is a copy of them: #! /usr/bin/env rdmd import std.stdio; struct X { int[] a; } void main() {

Why is it difficult to reference arrays in structs?

2019-10-02 Thread Brett via Digitalmars-d-learn
struct Y { double q; } struct X { Y[] a; } X x; auto r = x.a; r is not a reference?!?! When updating x.a, r is not updated ;/ [I'm not sure if it just creates a slice or what] Ok, fine! auto r = Now r is a reference, great!. But now the semantics of using the array completely change

Static Arrays in Structs/Classes and Dynamic Array Sizes

2016-01-18 Thread Dennis Croft via Digitalmars-d-learn
I'm trying to organize a large amount of simple data into manageable parcels of information that I can access efficiently. What I want to do is bake a static array into a class but be allowed to do the banking at runtime (because I want each array to be a different fixed length). Barring that,

Re: Static Arrays in Structs/Classes and Dynamic Array Sizes

2016-01-18 Thread tsbockman via Digitalmars-d-learn
On Monday, 18 January 2016 at 15:15:46 UTC, Dennis Croft wrote: I'm trying to organize a large amount of simple data into manageable parcels of information that I can access efficiently. What I want to do is bake a static array into a class but be allowed to do the banking at runtime (because

Re: Static Arrays in Structs/Classes and Dynamic Array Sizes

2016-01-18 Thread Marc Schütz via Digitalmars-d-learn
Here's what I suggest: alias T = int; class VariableLengthClass { private: string someMember; size_t length_; T[0] data_; public: static make(Args...)(size_t length, Args args) { static assert( typeof(this).init.data_.offsetof ==

Re: Arrays of structs

2015-08-27 Thread BBasile via Digitalmars-d-learn
On Thursday, 27 August 2015 at 11:45:14 UTC, anonymous wrote: On Thursday 27 August 2015 13:15, BBasile wrote: https://github.com/BBasile/iz/blob/master/import/iz/types.d#L125 https://github.com/BBasile/iz/blob/master/import/iz/types.d#L150

Re: Arrays of structs

2015-08-27 Thread BBasile via Digitalmars-d-learn
On Thursday, 27 August 2015 at 12:56:26 UTC, anonymous wrote: On Thursday 27 August 2015 14:35, BBasile wrote: Anyway. I cheat a bit with attributes but as long as it's only for me...I know this kinds of functions are not phobos-level. Sure, as long as you're cautious and regard those

Re: Arrays of structs

2015-08-27 Thread anonymous via Digitalmars-d-learn
On Thursday 27 August 2015 14:35, BBasile wrote: Anyway. I cheat a bit with attributes but as long as it's only for me...I know this kinds of functions are not phobos-level. Sure, as long as you're cautious and regard those functions as unsafe, you may be fine. You still risk accidentally

Arrays of structs

2015-08-27 Thread John Burton via Digitalmars-d-learn
I'm a c++ programmer trying to understand how memory allocation works in D. I created a struct and added a destructor to it. My understanding is that structs have deterministic destructors - they are called when the struct goes out of scope (unless it is allocated with new). Now if I put

Re: Arrays of structs

2015-08-27 Thread SimonN via Digitalmars-d-learn
Hi, On Thursday, 27 August 2015 at 10:05:31 UTC, John Burton wrote: understanding is that structs have deterministic destructors - they are called when the struct goes out of scope Yes, when they are declared right at the scope, and not contained in something that might live past the current

Re: Arrays of structs

2015-08-27 Thread BBasile via Digitalmars-d-learn
On Thursday, 27 August 2015 at 10:05:31 UTC, John Burton wrote: I'm a c++ programmer trying to understand how memory allocation works in D. I created a struct and added a destructor to it. My understanding is that structs have deterministic destructors - they are called when the struct goes

Re: Arrays of structs

2015-08-27 Thread John Burton via Digitalmars-d-learn
Ok that's great thank you. It's quite hard trying to get a proper understanding of memory allocation in D after years of C++ / RAII / unique_ptr / shared_ptr . I understand the details of course but it's going to take a while to really know it. Is there any way to explicitly free a dynamic

Re: Arrays of structs

2015-08-27 Thread John Burton via Digitalmars-d-learn
Thanks again for the updates. I've experimented some more and believe I understand. To be honest I'm finding it very hard to find the right idioms in D for safe and efficient programming when I'm so used to C++ / RAII everywhere. I'll adapt though :P

Re: Arrays of structs

2015-08-27 Thread Gary Willoughby via Digitalmars-d-learn
On Thursday, 27 August 2015 at 10:49:02 UTC, John Burton wrote: Thanks again for the updates. I've experimented some more and believe I understand. To be honest I'm finding it very hard to find the right idioms in D for safe and efficient programming when I'm so used to C++ / RAII

Re: Arrays of structs

2015-08-27 Thread Olivier Pisano via Digitalmars-d-learn
On Thursday, 27 August 2015 at 10:49:02 UTC, John Burton wrote: To be honest I'm finding it very hard to find the right idioms in D for safe and efficient programming when I'm so used to C++ / RAII everywhere. I'll adapt though :P This is true for every new language you learn. You first

Re: Arrays of structs

2015-08-27 Thread BBasile via Digitalmars-d-learn
On Thursday, 27 August 2015 at 10:49:02 UTC, John Burton wrote: Thanks again for the updates. I've experimented some more and believe I understand. To be honest I'm finding it very hard to find the right idioms in D for safe and efficient programming when I'm so used to C++ / RAII

Re: Arrays of structs

2015-08-27 Thread anonymous via Digitalmars-d-learn
On Thursday 27 August 2015 13:15, BBasile wrote: https://github.com/BBasile/iz/blob/master/import/iz/types.d#L125 https://github.com/BBasile/iz/blob/master/import/iz/types.d#L150 https://github.com/BBasile/iz/blob/master/import/iz/types.d#L191 Your use of @trusted is wrong and dangerous.

newbie confusion with arrays and structs

2012-01-26 Thread Robert Bernecky
Hi. I am trying to write my first D program, and am quite stuck in several areas. This is on Ubuntu 10.10 with the dmd DMD64 D Compiler v2.055. I want to create a generic struct for multi-dimensional arrays, in which the value parts can be shared. E.g., an array comprises a vector of its raveled

Re: newbie confusion with arrays and structs

2012-01-26 Thread H. S. Teoh
On Thu, Jan 26, 2012 at 08:34:12PM +, Robert Bernecky wrote: Hi. I am trying to write my first D program, and am quite stuck in several areas. This is on Ubuntu 10.10 with the dmd DMD64 D Compiler v2.055. [...] However, none of this stuff works. Here's what I get: dmd prd.d -unittest

Re: newbie confusion with arrays and structs

2012-01-26 Thread Jesse Phillips
The important mangled message here is: prd.o:(.data+0x250): undefined reference to `_D5Array12__ModuleInfoZ' This is a linker error, your code compiled fine. The English translation: There is an undefined reference to module Array You are compiling without including all files you need

Re: newbie confusion with arrays and structs

2012-01-26 Thread Robert Bernecky
Awesome! I feel much better now; thank you both for your instant help! dmd prd.d Array.d -unittest rbe@rattler:~/plural$ prd [0] [0, 1, 2, 3, 4] Created int iota shape is %d [0] iota value is %d [0, 1, 2, 3, 4] got to unittest for Array.d [0] [0, 1, 2, 3, 4] Created int [0] [0, 1, 2, 3, 4]

Re: newbie confusion with arrays and structs

2012-01-26 Thread Justin Whear
On Thu, 26 Jan 2012 21:53:39 +, Robert Bernecky wrote: Awesome! I feel much better now; thank you both for your instant help! dmd prd.d Array.d -unittest rbe@rattler:~/plural$ prd [0] [0, 1, 2, 3, 4] Created int iota shape is %d [0] iota value is %d [0, 1, 2, 3, 4] got to

Re: newbie confusion with arrays and structs

2012-01-26 Thread Jesse Phillips
On Thursday, 26 January 2012 at 22:10:23 UTC, Justin Whear wrote: If you want your Array code to be a separate library/project, you can compile it like so: dmd Array.d -lib Then when you compile your other project: dmd prd.d -I/location/of/Array.d -L/location/of/Array.so Justin It is