Comments (5)
I assume you're talking specifically x in dict
; there's no problem with unhashable in list
, obviously.
What if the concrete implementation of Mapping is a user-defined red/black tree? There's no problem with unhashable keys there either. Proposed additional checks must thus be restricted specifically to type *Dict, not any Mapping.
We could detect unhashable types by calling Hash before calling Get
Better still, after Get: the common case is no error; let errors be slow. If there's an error, try hashing. If hashing fails, report an error, otherwise let the error slide?
This would be a backward incompatible change to the Mapping.Get contract:
type Mapping interface {
...
// Get also defines the behavior of "v in mapping".
// The 'in' operator reports the 'found' component, ignoring errors.
Get(Value) (v Value, found bool, err error)
}
Side grumble: Hash is most unsatisfactory. I wish it took a seed parameter, used type uintptr, and was an optional method. Too late to change now though.
from starlark-go.
I assume you're talking specifically x in dict; there's no problem with unhashable in list, obviously.
Right. And also x in set
.
What if the concrete implementation of Mapping is a user-defined red/black tree?
The spec doesn't seem to indicate that this is supported:
The
in
operator reports whether its first operand is a member of its second operand, which must be a list, tuple, dict, set, or string.
I assume that this is an oversight. But the treatment of in
for user-defined Mappings appears to be unspecified at the moment.
The ideal scenario, to my mind, is that if the Get
fails because it calls Hash
and Hash
isn't supported, then you get an error. Not otherwise. A red/black would never call Hash
, so this yields reasonable behavior. Your suggestion about calling Hash
to diagnose a failed Get
achieves this behavior, I believe.
Better still, after Get: the common case is no error; let errors be slow. If there's an error, try hashing. If hashing fails, report an error, otherwise let the error slide?
That sounds sensible to me.
This would be a backward incompatible change to the Mapping.Get contract:
Bummer. This decision is above my pay grade.
Side grumble: Hash is most unsatisfactory. I wish it took a seed parameter, used type uintptr, and was an optional method. Too late to change now though.
Indeed.
from starlark-go.
If we cared enough about this to want to fix it, and wanted to finesse the backwards-compatibility issue, we could take the route of having a starlark.Unhashable
error type (probably just a struct wrapping an underlying error). If Get
returns a starlark.Unhashable
error, then we respect it. All other errors are ignored as they are right now. No existing implementations return such an error now, so this doesn't break any existing code, even though the docs have a semantic change. And then future Mapping implementations could opt in as they saw fit.
from starlark-go.
The Unhashable approach seems workable. I just did something very similar to add spellchecking to getAttr---see NoSuchAttrError. Care to send a CL? :)
from starlark-go.
Will do, using a "I'll work on it while my model is training" level SLA.
from starlark-go.
Related Issues (20)
- Anyway to check if a variable or functions exists in current environment? HOT 1
- Bug: incorrect `len()` for UTF-8 Chars HOT 1
- about reassignment limit
- Pass by value, or pass by reference? HOT 1
- s390x precision seems to be a little bit off in some operations HOT 3
- FR: Support More Methods for type `set` HOT 7
- Incomplete print() format, lack support of "%.02f" HOT 2
- Question about Security, Stack Overflows, and Runtime Panics when Running Custom Starlark Code HOT 1
- Bug in lib/time: time.parse_time can't support format like Python HOT 3
- Feature Request: Official Lib for `re`/`regex` HOT 1
- Caching Syntax Parsing Results for Scripts Run Repeatedly HOT 1
- Enhancement Request: Official Runes Library for Unicode Substring, Length, and String Manipulation HOT 6
- Thread-Safety for Built-in Types Such as Dict, List, Set HOT 1
- unexpected global variable referenced before assignment HOT 1
- Should repeated and map proto fields by writable by default? HOT 1
- Support for `struct.proto` well-known-type HOT 1
- time: allow to inject per-module or per-thread `now()` HOT 3
- Dict: support efficient key+value iteration HOT 1
- Why are the {C,ITER,}JMP instructions encoded with a 4-byte padding? HOT 2
- Passing starlark.Dict arguments in starlark.Call HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from starlark-go.