starlark icon indicating copy to clipboard operation
starlark copied to clipboard

dict() constructor should accept mappings

Open brandjon opened this issue 4 years ago • 1 comments

The current verbiage is:

dict creates a dictionary. It accepts up to one positional argument, which is interpreted as an iterable of two-element sequences (pairs), each specifying a key/value pair in the resulting dictionary.

In reality, the Java implementation allows the positional argument to also be another dictionary, in which case the entries of that dictionary are included in the new one. Note that this is a distinct case because iterating over dictionaries yields their keys, not their key-value pairs.

The Java implementation's behavior is consistent with Python. But in reality, it should be generalized to also extend to other user-defined mapping types. The concept of Mapping is already defined in the spec.

brandjon avatar Jun 24 '21 17:06 brandjon

For reference, in Python, the criteria for treating the positional argument to dict() as a mapping rather than an iterable appears to be that it has the keys() and __getitem__() methods.

brandjon avatar Jun 24 '21 17:06 brandjon