-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Operator '[]=' for the class 'Object' #543
Comments
The There are no plans to allow reflection directly on Aside from that, it would break every other class which currently extend |
@LeandroDoldan Something like this might work for you: // T is the type of things held, instead of the type of the collection.
class Updater<T> {
Map<String, T> entity;
Updater(this.entity);
update(String key, T value) {
entity[key] = value;
}
} You could write dynamic code that is against the spirit of Dart. I would advise against this - dynamic invocation is often less efficient and you get no safety or code completion benefits that you would get with stronger typing. class Updater<T> {
T entity;
Updater(this.entity);
update(String key, T value) {
(entity as dynamic)[key] = value;
}
} I do think it is an interesting idea to 'destructure' the type. If Dart had that feature it might look like class Updater<T extends Map<K, V>> {
T entity;
Updater(this.entity);
update(K key, V value) {
entity[key] = value;
}
} @lrhn Sometimes it is awkward to 'pass around' several types as type parameters, perhaps the language could support that better. |
@rakudrama, just FYI, the 'destructure the type' feature is at the core of the type patterns proposal (#170). That proposal is intended to be used by other constructs, e.g., a pattern matching void foo(Object x, Object y, Object z) {
if (x is Map<var K, var V>) {
// K, V denote exactly the value of the type arguments to the type of `x` at `Map`,
// so this is safe:
if (y is K && z is V) x[y] = z;
}
} A similar operation for the relationship between types could certainly be provided ( In the end we did not use type patterns for extension methods, but extension methods are still similar. (Basically, we didn't need type patterns because we decided to match extension type arguments with the statically known values, not the dynamic ones, and then the job can be done using standard type inference.) So you can use extensions for some static cases: extension Updater<K, V> on Map<K, V> {
void myUpdate(K key, V value) => this[key] = value;
}
main() {
Map<int, int> map = {};
map.myUpdate(3, 4);
} This would also work in the case where the type of |
@LeandroDoldan
I think the question that we should first and foremost ask here is: why do you want such pattern in the first place? Can you give a larger example on how you are planning to use such |
@Vatsboy, your error isn't related to this issue. Try posting it on StackOverflow.com for an explanation of your error. |
Problem
AFAIK, something like the following is impossible to implement:
Because the following error is thrown:
Request
Please, implement the operator '[]=' for the class 'Object'.
Regarding typing, this is a simple TypeScript-like suggestion:
Thank you!
The text was updated successfully, but these errors were encountered: