Re: Feature to get or add value to an associative array.

2018-04-25 Thread Giles Bathgate via Digitalmars-d
On Tuesday, 24 April 2018 at 09:09:37 UTC, Andrea Fontana wrote: Just one question: does this work for value-types? The intention is that it will work for value types. I will add some unit tests to check that use case. - Giles

Re: Feature to get or add value to an associative array.

2018-04-24 Thread Andrea Fontana via Digitalmars-d
On Sunday, 15 April 2018 at 22:52:47 UTC, Giles Bathgate wrote: Hi, I wanted a way to lazily insert a value into an associative array, and I am proposing a new function called getOrAdd in https://github.com/dlang/druntime/pull/2162 Yes please :) Just one question: does this work for

Re: Why don't lazy parameters bind to delegates? Was: Feature to get or add value to an associative array.

2018-04-20 Thread Jonathan M Davis via Digitalmars-d
On Friday, April 20, 2018 19:36:57 Steven Schveighoffer via Digitalmars-d wrote: > On 4/20/18 5:40 PM, Jonathan M Davis wrote: > > On Friday, April 20, 2018 16:35:43 Steven Schveighoffer via > > Digitalmars-d > > > > wrote: > >> On 4/17/18 4:49 PM, Steven Schveighoffer wrote: > >>> On 4/17/18

Re: Why don't lazy parameters bind to delegates? Was: Feature to get or add value to an associative array.

2018-04-20 Thread Jonathan M Davis via Digitalmars-d
On Friday, April 20, 2018 19:27:20 Steven Schveighoffer via Digitalmars-d wrote: > On 4/20/18 6:46 PM, Giles Bathgate wrote: > > On Friday, 20 April 2018 at 22:21:13 UTC, Jonathan M Davis wrote: > >> Honestly, I think that it's a terrible idea to special-case it like > >> that. If we want to

Re: Why don't lazy parameters bind to delegates? Was: Feature to get or add value to an associative array.

2018-04-20 Thread Steven Schveighoffer via Digitalmars-d
On 4/20/18 7:21 PM, Giles Bathgate wrote: On Friday, 20 April 2018 at 23:13:58 UTC, Jonathan M Davis wrote: Yes. That should work. e.g. this import std.stdio; void foo(lazy string l) { } void main() {     foo({writeln("foo"); return "str";}()); } compiles and runs just fine without printing

Re: Why don't lazy parameters bind to delegates? Was: Feature to get or add value to an associative array.

2018-04-20 Thread Steven Schveighoffer via Digitalmars-d
On 4/20/18 5:40 PM, Jonathan M Davis wrote: On Friday, April 20, 2018 16:35:43 Steven Schveighoffer via Digitalmars-d wrote: On 4/17/18 4:49 PM, Steven Schveighoffer wrote: On 4/17/18 12:18 PM, Nick Treleaven wrote: Thanks for making this pull, I've thought about solving this before. I think

Re: Why don't lazy parameters bind to delegates? Was: Feature to get or add value to an associative array.

2018-04-20 Thread Steven Schveighoffer via Digitalmars-d
On 4/20/18 6:46 PM, Giles Bathgate wrote: On Friday, 20 April 2018 at 22:21:13 UTC, Jonathan M Davis wrote: Honestly, I think that it's a terrible idea to special-case it like that. If we want to argue for making it work in the language, that's fine, but if we special-case it like this, then

Re: Why don't lazy parameters bind to delegates? Was: Feature to get or add value to an associative array.

2018-04-20 Thread Giles Bathgate via Digitalmars-d
On Friday, 20 April 2018 at 23:13:58 UTC, Jonathan M Davis wrote: Yes. That should work. e.g. this import std.stdio; void foo(lazy string l) { } void main() { foo({writeln("foo"); return "str";}()); } compiles and runs just fine without printing anything, whereas you get a compilation

Re: Why don't lazy parameters bind to delegates? Was: Feature to get or add value to an associative array.

2018-04-20 Thread Jonathan M Davis via Digitalmars-d
On Friday, April 20, 2018 22:46:54 Giles Bathgate via Digitalmars-d wrote: > On Friday, 20 April 2018 at 22:21:13 UTC, Jonathan M Davis wrote: > > Honestly, I think that it's a terrible idea to special-case it > > like that. If we want to argue for making it work in the > > language, that's fine,

Re: Why don't lazy parameters bind to delegates? Was: Feature to get or add value to an associative array.

2018-04-20 Thread Giles Bathgate via Digitalmars-d
On Friday, 20 April 2018 at 22:21:13 UTC, Jonathan M Davis wrote: Honestly, I think that it's a terrible idea to special-case it like that. If we want to argue for making it work in the language, that's fine, but if we special-case it like this, then it will work with some functions that have

Re: Why don't lazy parameters bind to delegates? Was: Feature to get or add value to an associative array.

2018-04-20 Thread Jonathan M Davis via Digitalmars-d
On Friday, April 20, 2018 22:03:04 Giles Bathgate via Digitalmars-d wrote: > On Friday, 20 April 2018 at 20:35:43 UTC, Steven Schveighoffer > > wrote: > > Let me say I was surprised that this doesn't actually work. An > > in-line lambda will NOT work as a lazy parameter, even though > > that's

Re: Why don't lazy parameters bind to delegates? Was: Feature to get or add value to an associative array.

2018-04-20 Thread Giles Bathgate via Digitalmars-d
On Friday, 20 April 2018 at 20:35:43 UTC, Steven Schveighoffer wrote: Let me say I was surprised that this doesn't actually work. An in-line lambda will NOT work as a lazy parameter, even though that's EXACTLY what a lazy parameter is implemented as! And variadic lazy parameters are

Re: Why don't lazy parameters bind to delegates? Was: Feature to get or add value to an associative array.

2018-04-20 Thread Jonathan M Davis via Digitalmars-d
On Friday, April 20, 2018 16:35:43 Steven Schveighoffer via Digitalmars-d wrote: > On 4/17/18 4:49 PM, Steven Schveighoffer wrote: > > On 4/17/18 12:18 PM, Nick Treleaven wrote: > >> Thanks for making this pull, I've thought about solving this before. I > >> think the function needs to provide a

Why don't lazy parameters bind to delegates? Was: Feature to get or add value to an associative array.

2018-04-20 Thread Steven Schveighoffer via Digitalmars-d
On 4/17/18 4:49 PM, Steven Schveighoffer wrote: On 4/17/18 12:18 PM, Nick Treleaven wrote: Thanks for making this pull, I've thought about solving this before. I think the function needs to provide a way to tell if the value was already present. Not as straightforward, but it can be done:

Re: Feature to get or add value to an associative array.

2018-04-20 Thread Uknown via Digitalmars-d
On Friday, 20 April 2018 at 15:00:39 UTC, Steven Schveighoffer wrote: On 4/20/18 10:34 AM, Uknown wrote: On Friday, 20 April 2018 at 14:02:17 UTC, Steven Schveighoffer wrote: On 4/20/18 4:24 AM, Nick Treleaven wrote: On Wednesday, 18 April 2018 at 16:47:50 UTC, ag0aep6g wrote: [...] Sorry,

Re: Feature to get or add value to an associative array.

2018-04-20 Thread Steven Schveighoffer via Digitalmars-d
On 4/20/18 10:34 AM, Uknown wrote: On Friday, 20 April 2018 at 14:02:17 UTC, Steven Schveighoffer wrote: On 4/20/18 4:24 AM, Nick Treleaven wrote: On Wednesday, 18 April 2018 at 16:47:50 UTC, ag0aep6g wrote: You can get a pointer from the ref return:     Value* p = ("key", { inserted = true;

Re: Feature to get or add value to an associative array.

2018-04-20 Thread Uknown via Digitalmars-d
On Friday, 20 April 2018 at 14:02:17 UTC, Steven Schveighoffer wrote: On 4/20/18 4:24 AM, Nick Treleaven wrote: On Wednesday, 18 April 2018 at 16:47:50 UTC, ag0aep6g wrote: You can get a pointer from the ref return:     Value* p = ("key", { inserted = true; return Value.init; }); This is

Re: Feature to get or add value to an associative array.

2018-04-20 Thread Steven Schveighoffer via Digitalmars-d
On 4/20/18 4:24 AM, Nick Treleaven wrote: On Wednesday, 18 April 2018 at 16:47:50 UTC, ag0aep6g wrote: You can get a pointer from the ref return:     Value* p = ("key", { inserted = true; return Value.init; }); This is not @safe, even with -dip1000: Error: cannot take address of ref return

Re: Feature to get or add value to an associative array.

2018-04-20 Thread Giles Bathgate via Digitalmars-d
On Friday, 20 April 2018 at 02:12:37 UTC, Jonathan M Davis wrote: Out of all of those, I _really_ hope that you don't go with require. It sounds like the sort of thing that you'd get in a library having to do with unit testing or contracts and gives no indication whatsoever as to what it does.

Re: Feature to get or add value to an associative array.

2018-04-20 Thread Giles Bathgate via Digitalmars-d
On Friday, 20 April 2018 at 08:37:59 UTC, Nick Treleaven wrote: Again, would the delegate calls always be inlined, in all cases? I think having a low-level API in druntime is appropriate, it's a runtime library after all. So the low-level API you are requesting is part of the pull request,

Re: Feature to get or add value to an associative array.

2018-04-20 Thread ag0aep6g via Digitalmars-d
On Friday, 20 April 2018 at 09:24:26 UTC, Jonathan M Davis wrote: The compiler assumes that a pointer is valid when determining whether code is @safe. [...] Basically, it verifies the @safety of pointers when they're created and then assumes that they're @safe after that. Can't it do the

Re: Feature to get or add value to an associative array.

2018-04-20 Thread Jonathan M Davis via Digitalmars-d
On Friday, April 20, 2018 10:56:37 ag0aep6g via Digitalmars-d wrote: > On 04/20/2018 10:24 AM, Nick Treleaven wrote: > > On Wednesday, 18 April 2018 at 16:47:50 UTC, ag0aep6g wrote: > >> You can get a pointer from the ref return: > >> > >> Value* p = ("key", { inserted = true; return

Re: Feature to get or add value to an associative array.

2018-04-20 Thread ag0aep6g via Digitalmars-d
On 04/20/2018 10:24 AM, Nick Treleaven wrote: On Wednesday, 18 April 2018 at 16:47:50 UTC, ag0aep6g wrote: You can get a pointer from the ref return:     Value* p = ("key", { inserted = true; return Value.init; }); This is not @safe, even with -dip1000: Error: cannot take address of ref

Re: Feature to get or add value to an associative array.

2018-04-20 Thread Dominikus Dittes Scherkl via Digitalmars-d
On Thursday, 19 April 2018 at 08:20:02 UTC, Giles Bathgate wrote: On Wednesday, 18 April 2018 at 21:04:53 UTC, Jordan Wilson wrote: Thinking seems sound, although having a name starting with "get" does have the advantage of being more related to the existing get. Ah yes, good point. I think

Re: Feature to get or add value to an associative array.

2018-04-20 Thread Nick Treleaven via Digitalmars-d
On Wednesday, 18 April 2018 at 16:04:13 UTC, Giles Bathgate wrote: I understand where you are coming from, but I am not sure it is appropriate to shoehorn every use case into one api. It is not shoehorning, the difference here is just returning by ref vs pointer. I understand that, as the

Re: Feature to get or add value to an associative array.

2018-04-20 Thread Nick Treleaven via Digitalmars-d
On Wednesday, 18 April 2018 at 16:47:50 UTC, ag0aep6g wrote: You can get a pointer from the ref return: Value* p = ("key", { inserted = true; return Value.init; }); This is not @safe, even with -dip1000: Error: cannot take address of ref return of f() in @safe function main

Re: Feature to get or add value to an associative array.

2018-04-19 Thread Jonathan M Davis via Digitalmars-d
On Wednesday, April 18, 2018 17:19:45 Giles Bathgate via Digitalmars-d wrote: > On Sunday, 15 April 2018 at 22:55:41 UTC, Giles Bathgate wrote: > > Time for a bikeshed discussion... > > I have had some thoughts about the name and would like to share > my idea. > > Firstly to summarise here are

Re: Feature to get or add value to an associative array.

2018-04-19 Thread Giles Bathgate via Digitalmars-d
On Wednesday, 18 April 2018 at 21:04:53 UTC, Jordan Wilson wrote: Thinking seems sound, although having a name starting with "get" does have the advantage of being more related to the existing get. Ah yes, good point. I think now we've had the discussion about other use cases though that

Re: Feature to get or add value to an associative array.

2018-04-18 Thread Jordan Wilson via Digitalmars-d
On Wednesday, 18 April 2018 at 17:19:45 UTC, Giles Bathgate wrote: On Sunday, 15 April 2018 at 22:55:41 UTC, Giles Bathgate wrote: Time for a bikeshed discussion... I have had some thoughts about the name and would like to share my idea. Firstly to summarise here are the names that have

Re: Feature to get or add value to an associative array.

2018-04-18 Thread Giles Bathgate via Digitalmars-d
On Sunday, 15 April 2018 at 22:55:41 UTC, Giles Bathgate wrote: Time for a bikeshed discussion... I have had some thoughts about the name and would like to share my idea. Firstly to summarise here are the names that have been found/discussed so far: C# : GetOrAdd Java

Re: Feature to get or add value to an associative array.

2018-04-18 Thread ag0aep6g via Digitalmars-d
On 04/18/2018 11:41 AM, Nick Treleaven wrote: How do you implement this if the function returns with ref: bool inserted; auto p = aa.slot("key", ); if (inserted) {   ...   // set *p } else {   // read *p   ...   // set *p } You can get a pointer from the ref return: Value* p =

Re: Feature to get or add value to an associative array.

2018-04-18 Thread Giles Bathgate via Digitalmars-d
On Wednesday, 18 April 2018 at 09:41:48 UTC, Nick Treleaven wrote: How do you implement this if the function returns with ref: I understand where you are coming from, but I am not sure it is appropriate to shoehorn every use case into one api. I think actually what you are describing here is

Re: Feature to get or add value to an associative array.

2018-04-18 Thread Nick Treleaven via Digitalmars-d
On Tuesday, 17 April 2018 at 20:49:30 UTC, Steven Schveighoffer wrote: Why do you think it's less efficient to use a lazy parameter? Wouldn't an extra function call have to happen, at least in some cases? This pattern needs a pointer to be returned, instead of using `ref`. Note that `` is

Re: Feature to get or add value to an associative array.

2018-04-17 Thread Giles Bathgate via Digitalmars-d
On Tuesday, 17 April 2018 at 21:40:55 UTC, Giles Bathgate wrote: Rust calls its version of this function `or_insert_with` (blegh) Of course, a rustic API could be built atop this PR: template entry(K, V) { static struct Entry { alias get this; V[K] aa; K key;

Re: Feature to get or add value to an associative array.

2018-04-17 Thread Giles Bathgate via Digitalmars-d
On Tuesday, 17 April 2018 at 20:49:30 UTC, Steven Schveighoffer wrote: Not as straightforward, but it can be done: bool inserted = false; auto p = aa.getOrAdd("key", {inserted = true; return new Person; }); Yes, I like that approach. I don't want to bloat the feature at this stage, although

Re: Feature to get or add value to an associative array.

2018-04-17 Thread Steven Schveighoffer via Digitalmars-d
On 4/17/18 12:18 PM, Nick Treleaven wrote: On Sunday, 15 April 2018 at 22:52:47 UTC, Giles Bathgate wrote: The function provides a means to get a value corresponding to the key, but if the value doesn't exist it will evaluate the lazy argument to create a new value, add this to the associative

Re: Feature to get or add value to an associative array.

2018-04-17 Thread Giles Bathgate via Digitalmars-d
On Tuesday, 17 April 2018 at 19:33:16 UTC, MrSmith wrote: You may need to perform extra logic when value is inserted in the container and something else when value already existed. Fair enough, I will consider adding this and some tests to the PR, a function overload seems like the way to go.

Re: Feature to get or add value to an associative array.

2018-04-17 Thread MrSmith via Digitalmars-d
On Tuesday, 17 April 2018 at 17:27:02 UTC, Giles Bathgate wrote: I like the name. I think your version is quite low level which ultimately provides more power at the expense of making the callee code less clean. I am not sure with D which of those two aspects is preferred. Perhaps both

Re: Feature to get or add value to an associative array.

2018-04-17 Thread Giles Bathgate via Digitalmars-d
On Tuesday, 17 April 2018 at 16:18:32 UTC, Nick Treleaven wrote: I called the function `slot` because it always returns the address of the slot which the value is stored in. I like the name. I think your version is quite low level which ultimately provides more power at the expense of making

Re: Feature to get or add value to an associative array.

2018-04-17 Thread Nick Treleaven via Digitalmars-d
On Sunday, 15 April 2018 at 22:52:47 UTC, Giles Bathgate wrote: The function provides a means to get a value corresponding to the key, but if the value doesn't exist it will evaluate the lazy argument to create a new value, add this to the associative array and then return it. auto p =

Re: Feature to get or add value to an associative array.

2018-04-17 Thread Giles Bathgate via Digitalmars-d
On Tuesday, 17 April 2018 at 09:21:14 UTC, Giles Bathgate wrote: The java name for such a function is `computeIfAbsent` More names from other languages, this time python: https://docs.python.org/3/library/stdtypes.html#dict.setdefault It's horrid though, a method called `set` that returns a

Re: Feature to get or add value to an associative array.

2018-04-17 Thread Steven Schveighoffer via Digitalmars-d
On 4/15/18 6:52 PM, Giles Bathgate wrote: I find this later code clunky and it requires two hashes/lookups. The proposed implementation adds support directly to rt/aaA.d to avoid this. I should also point out that the allocation of a new Person in the example is trivial, but it might often be

Re: Feature to get or add value to an associative array.

2018-04-17 Thread Giles Bathgate via Digitalmars-d
On Tuesday, 17 April 2018 at 07:40:23 UTC, Giles Bathgate wrote: My personal reason for not liking it is because the same name is used by Microsoft The java name for such a function is `computeIfAbsent`

Re: Feature to get or add value to an associative array.

2018-04-17 Thread Giles Bathgate via Digitalmars-d
On Tuesday, 17 April 2018 at 00:04:32 UTC, Cym13 wrote: "in" returns a pointer to the object, there'es not double lookup necessary: // if we don't know .get(key, default) exists auto ptr = key in aa; auto value = ptr ? *ptr : default; This doesn't work. `in` returns null when

Re: Feature to get or add value to an associative array.

2018-04-16 Thread Nicholas Wilson via Digitalmars-d
On Tuesday, 17 April 2018 at 00:04:32 UTC, Cym13 wrote: auto value = ptr ? *ptr : *ptr = default; That is going to crash if ptr is null. is a new flag/method really that necessary? In my experience if you have trouble naming it you haven't found its true purpose yet. I think

Re: Feature to get or add value to an associative array.

2018-04-16 Thread Cym13 via Digitalmars-d
On Monday, 16 April 2018 at 18:59:54 UTC, Giles Bathgate wrote: On Monday, 16 April 2018 at 12:41:07 UTC, JN wrote: It's only one additional "in", but makes the code more explicit and clear. I think in most cases, you will want to check if you are dealing with a fetched object or a default

Re: Feature to get or add value to an associative array.

2018-04-16 Thread user1234 via Digitalmars-d
On Sunday, 15 April 2018 at 22:55:41 UTC, Giles Bathgate wrote: On Sunday, 15 April 2018 at 22:52:47 UTC, Giles Bathgate wrote: I am proposing a new function called getOrAdd in I posted details of the PR here because at least 2 people do not like the name getOrAdd (and one of those people

Re: Feature to get or add value to an associative array.

2018-04-16 Thread Giles Bathgate via Digitalmars-d
On Monday, 16 April 2018 at 12:41:07 UTC, JN wrote: It's only one additional "in", but makes the code more explicit and clear. I think in most cases, you will want to check if you are dealing with a fetched object or a default created one, so that will complicate the function even further.

Re: Feature to get or add value to an associative array.

2018-04-16 Thread JN via Digitalmars-d
On Sunday, 15 April 2018 at 22:52:47 UTC, Giles Bathgate wrote: Hi, I wanted a way to lazily insert a value into an associative array, and I am proposing a new function called getOrAdd in https://github.com/dlang/druntime/pull/2162 I am not sure if it's a good idea to combine accessor and

Re: Feature to get or add value to an associative array.

2018-04-16 Thread bauss via Digitalmars-d
On Monday, 16 April 2018 at 03:42:18 UTC, Jordan Wilson wrote: On Sunday, 15 April 2018 at 22:55:41 UTC, Giles Bathgate wrote: On Sunday, 15 April 2018 at 22:52:47 UTC, Giles Bathgate wrote: I am proposing a new function called getOrAdd in I posted details of the PR here because at least 2

Re: Feature to get or add value to an associative array.

2018-04-16 Thread Giles Bathgate via Digitalmars-d
On Monday, 16 April 2018 at 03:42:18 UTC, Jordan Wilson wrote: I think Adding a Yes.add flag to the existing get makes sense That's an interesting idea. I forgot to mention that getOrAdd is a mutable function whereas get is inout, so that might also be problematic.

Re: Feature to get or add value to an associative array.

2018-04-15 Thread Jordan Wilson via Digitalmars-d
On Sunday, 15 April 2018 at 22:55:41 UTC, Giles Bathgate wrote: On Sunday, 15 April 2018 at 22:52:47 UTC, Giles Bathgate wrote: I am proposing a new function called getOrAdd in I posted details of the PR here because at least 2 people do not like the name getOrAdd (and one of those people

Re: Feature to get or add value to an associative array.

2018-04-15 Thread Giles Bathgate via Digitalmars-d
On Sunday, 15 April 2018 at 22:52:47 UTC, Giles Bathgate wrote: I am proposing a new function called getOrAdd in I posted details of the PR here because at least 2 people do not like the name getOrAdd (and one of those people being myself) Time for a bikeshed discussion...

Feature to get or add value to an associative array.

2018-04-15 Thread Giles Bathgate via Digitalmars-d
Hi, I wanted a way to lazily insert a value into an associative array, and I am proposing a new function called getOrAdd in https://github.com/dlang/druntime/pull/2162 The function provides a means to get a value corresponding to the key, but if the value doesn't exist it will evaluate the