-
Notifications
You must be signed in to change notification settings - Fork 214
Add infer keyword #2173
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
Comments
Should type bounds really be type patterns? class EntityStore<T extends Entity<var K>> { ... } |
We've had similar discussions previously, e.g., #620 (comment). |
That's a really interesting idea. What about in parameter lists? foo(List<final T> someList) { ... } Deciding when to use a generic method and when to use a type pattern seems like a tricky and subtle design choice. |
My initial take would be to avoid this, and instead add the first class version of |
The distinction between The pattern-in-parameter is no different from doing There is no way to separate the The next question would be whether you can do We probably can allow the return type to depend on the pattern-matched type variable, like It might very well be that some generic methods would be better written as pattern-matching type-extractors. So, a pattern like |
Yeah, the patterns proposal already covers this use case. But thinking about using type patterns in bounds made me wonder about using them in other places where type parameters can occur when binding is happening and parameter lists are another example of that. (This, of course, also raises the question of allowing other kinds of patterns in parameter lists.)
Yeah, I think this is the right approach. Allowing type patterns directly in the parameter list seems like a can of worms. |
I was actually using that as an argument for allowing patterns in parameter lists. Pattern-parameters are statically typed irrefutable binding constructs, just like what variable declarations become with patterns, so I don't think there should be any problem, and forcing people to declare the un-patterned variable and then immediately pattern-match it inside the function, is just adding overhead. |
I think we should close this in favor of #620 as they address the exact same issue but with different proposed solutions. Or the other way around. |
btw, is this ticket (or #620) something that is being considered/talked about by the team? 👀 |
Cross referencing with the issue related to patterns #2221 |
In my use case, I want to wrap a list in generic type with a class without knowing what inner type is. T wrap(T obj) {
if (obj is List) obj = ReactiveList(obj) as T
return obj;
} where Resulting in:
Is it possible to infer the type? T wrap(T obj) {
if (obj is List<var V>) obj = ReactiveList<V>(obj) as T
return obj;
} |
@shtse8 Why not just ReactiveList<T> wrap<T>(Iterable<T> list) {
return ReactiveList(list);
} |
because T is probably other types, not only list. class Ref<T> {
T value;
Ref(T value) : this.value = _wrap(value);
T _wrap(T obj) {
if (obj is List) obj = ReactiveList(obj) as T
if (obj is Map) obj = ReactiveMap(obj) as T
return obj;
}
} |
Uh oh!
There was an error while loading. Please reload this page.
Let:
Calling Code:
final store = EntityStore<Customer, int>();
But type parameter
K
could already be inferred fromT
:final store = EntityStore<Customer>(); // this doesn't work
I propose
infer
keyword for type parameters that could be inferred from other type parameters:class EntityStore<T extends Entity<K>, infer K> {}
The text was updated successfully, but these errors were encountered: