How to update Associative Array?
Ali Çehreli
acehreli at yahoo.com
Sun Feb 13 01:27:45 UTC 2022
On 2/12/22 13:37, kdevel wrote:
> On Thursday, 10 February 2022 at 17:09:23 UTC, Ali Çehreli wrote:
>> import std.traits : isAssociativeArray, isImplicitlyConvertible,
>> KeyType, ValueType;
>>
>> void update(Target, From)(ref Target target, From from)
>> if (isAssociativeArray!Target &&
>> isAssociativeArray!From &&
>> isImplicitlyConvertible!(KeyType!From, KeyType!Target) &&
>> isImplicitlyConvertible!(ValueType!From, ValueType!Target))
>> {
>> foreach (kv; from.byKeyValue) {
>> target[kv.key] = kv.value;
>> }
>> }
>
> [...]
>
>> Yes, it may look scary to newcomers to D but the template constraint
>> is just for improved usability.
>
> It also looks scary to me and I use D now for quite a while. Assume I
> have this client code:
>
> string[int] q;
> byte[short] r;
> q.update (r);
So, that code is not valid because a 'byte' cannot implicitly be
converted to a 'string'.
> It produces this error message from your version:
>
> $ dmd -checkaction=context -unittest -run v1
> v1.d(21): Error: template `v1.update` cannot deduce function from
> argument types `!()(string[int], byte[short])`
> v1.d(3): Candidate is: `update(Target, From)(ref Target
> target, From from)`
> with `Target = string[int],
> From = byte[short]`
> must satisfy the following constraint:
> ` isImplicitlyConvertible!(ValueType!From, ValueType!Target)`
That looks like a wall of text but is much better than before. Now, the
compiler is telling us what requirement could not be met.
Additionally, the compilation fail in user code; telling the user why
their code is not valid.
> If I remove the constraint (the if-stuff) I get
>
> v1.d(12): Error: cannot implicitly convert expression `kv.value()`
> of type `byte` to `string`
> v1.d(23): Error: template instance `v1.update!(string[int],
> byte[short])` error instantiating
That's inferior because now the error message is pointing at the
implementation of a library function and confuses everyone. We have such
functions in Phobos.
> Can this really be improved?
I am not sure.
Here is an idea, which doesn't seem to add much as is:
'if' constraints could have a message for an improved error message like
"This function template could not be used because 'byte' cannot
implicitly be converted to 'string'." Note that there could be many
candidates like "And this function template could not be used because 42
is not prime", etc. The programmer would read all those and figure out
usable parameters.
But I think those would just be translations of the error messages.
Another idea is perhaps the compiler could resolve ValueType!From to
'byte' and provide the more readable
isImplicitlyConvertible!(byte, string)
I don't know how feasible that is.
Still, I am glad the error messages today are much better. After all,
everything that the programmer needs is spelled out here. It requires
following some types:
with `Target = string[int],
From = byte[short]`
must satisfy the following constraint:
isImplicitlyConvertible!(ValueType!From, ValueType!Target)`
The only thing that's not readily available there is ValueType but at
least it's very readable.
Sorry, I rambled but I'm under the impression that this complexity is
inherent.
Ali
More information about the Digitalmars-d-learn
mailing list