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
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
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
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
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
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
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
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
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,
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
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
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
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
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:
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,
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;
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
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
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.
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,
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
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
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
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
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
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
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
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
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
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
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 =
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
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
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;
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
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
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.
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
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
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 =
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
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
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`
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
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
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
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
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.
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
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
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.
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
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...
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
54 matches
Mail list logo