Re: Get pointer or reference of an element in Array(struct)
On Saturday, 9 December 2017 at 19:26:26 UTC, David Nadlinger wrote: but "free" references don't exist in the language. To the point! Thanks!
Re: Get pointer or reference of an element in Array(struct)
On Saturday, 9 December 2017 at 06:46:27 UTC, Arun Chandrasekaran wrote: On Saturday, 9 December 2017 at 06:38:46 UTC, anonymous wrote: On Saturday, 9 December 2017 at 06:15:16 UTC, Arun Chandrasekaran wrote: Is there a way to get the pointer or reference of an element in Array(T)? [...] auto d2 = gallery[0]; auto d2 = &gallery[0]; Thanks. Just curious why reference can't be obtained here. Saves nasty null checks in most places. In D, structs are passed by value unless otherwise specified. Using a class will yield the expected result.
Re: Get pointer or reference of an element in Array(struct)
On Saturday, 9 December 2017 at 06:46:27 UTC, Arun Chandrasekaran wrote: Thanks. Just curious why reference can't be obtained here. Saves nasty null checks in most places. D simply doesn't have a (C++-style) concept of references as part of the type. Arguments can be passed by reference - hence the `ref` keyword -, but "free" references don't exist in the language. The ref in foreach loop variables can be conceptually thought of as a parameter to the loop body as well. (For opApply-based iteration, the loop body indeed gets turned into a function; for "plain" iteration the compiler AST internally has special ref variables, but they are not visible to the language.) -David
Re: Get pointer or reference of an element in Array(struct)
On Saturday, 9 December 2017 at 06:38:46 UTC, anonymous wrote: On Saturday, 9 December 2017 at 06:15:16 UTC, Arun Chandrasekaran wrote: Is there a way to get the pointer or reference of an element in Array(T)? [...] auto d2 = gallery[0]; auto d2 = &gallery[0]; Thanks. Just curious why reference can't be obtained here. Saves nasty null checks in most places.
Re: Get pointer or reference of an element in Array(struct)
On Saturday, 9 December 2017 at 06:15:16 UTC, Arun Chandrasekaran wrote: Is there a way to get the pointer or reference of an element in Array(T)? [...] auto d2 = gallery[0]; auto d2 = &gallery[0];
Get pointer or reference of an element in Array(struct)
Is there a way to get the pointer or reference of an element in Array(T)? https://run.dlang.io/gist/70fd499afe8438d4877f57aec90c3091?compiler=dmd The assertion seems to fail below. Value copy is not is intended here. module test; void main() { struct Data { int id; } import std.container.array; Array!Data gallery; Data d1; gallery.insertBack(d1); auto d2 = gallery[0]; d2.id = 1; assert(d2.id == gallery[0].id, "neither ref nor pointer"); }