September 19, 2019
On Wednesday, 18 September 2019 at 08:24:17 UTC, Daniel Kozak wrote:
> On Tue, Sep 17, 2019 at 8:15 PM Brett via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
>>
>> ...
>>
>> Alternatively, have a keys for a DA:
>>
>> then one can do
>>
>> if (y in x.keys)
>>
>> and this is far more expressive so there is no confusion and also
>> works with AA's(and would not result in keys having to be created
>> because it is semantically the same as (y in x) if x is an AA).
>>
> No this would not work, because on AAs (keys) it will return dynamic
> array and you are back with same issue.
> You will need to use something with different name and write some UFCS
> helper for both DA and AA, for AA it could just return itself, for DA
> you can use Adam's example

No, because x.keys would return EXACTLY a dynamic array and not work(exactly as you said)... so the compiler knows that it works!

That is, x.keys is a semantic rule to check in the keys, it actually does not return any dynamic array ever! Because of that the compiler can make the semantic work.

y in x.keys

is ALWAYS invalid! Right?

For arrays, for associative arrays and that is all it works on!

So if it is always invalid, it can always be made invalid(to rewrite the semantic).

There can be no misinterpretation because it's always invalid.

For example, to make it clear too you, what if we extended the compiler so it looked like this:

y in x#keys

Then could it work? Could the semantic make sense to you? Sure it changes the language, but imagine a D 3.4383 where it was added, possible?

The compiler would have no issue extending the grammar to accept x.keys as a semantic and to do "magic" and people that learn the in keyword would just have to learn how it works, simple as that.

That is, WE DEFINE `a in x.keys` to be a range check. This then works for DA's because x.keys just is an "alias" to x and x.keys is just a DA when x is an AA(to to the first case).

To make it clear:

we want to do

y in x

for DA, right? But we can't because this can be confusing.

so we make a rule that when we have the syntax

y in x.keys

it does a range check as if the first case, but it is not confusing now.

but if x is an AA, x.keys returns a DA, and now we are back in the first case...

so we do

y in x.keys.keys

but still in the first case... but now the compiler sees, effectively

y in (x.keys).keys

and knows to do a range check ala the first case...

but it is redundant to write x.keys.keys so it can be simplified.

It works, trust me, think about it!



1 2 3
Next ›   Last »