Comments (4)
issues such as #620 could be resolved using extensions.
Good point, @rrousselGit!
Actually, an 'existential open' operation would suffice, so we wouldn't have to make huge breaking changes to the semantics of extension
declarations:
class Unary<A> {}
void doSomething<T extends Unary<Object?>>(T unary) {
unary._doSomething();
}
extension on Unary<Object?> {
void _doSomething() {
final self = this; // We need this until we get promotion of `this`.
if (self is Unary<final X>) {
// In this scope we have access to the actual value of the type
// argument `X` where `self` has run-time type `Unary<X>`.
X x = ...; // We can use `X` as a type.
print('use $X'); // Of course, we can also obtain a `Type` from it.
}
}
}
The construct self is Unary<final X>
is the existential open. It is not intended to be an entire type pattern matching feature, it's just a simple mechanism which is able to look up the value of a type variable in the type of an object.
This is not surprising, I just think it's worth mentioning that we won't have to make radical changes to the semantics of existing mechanisms in order to provide access to this kind of functionality.
from language.
Here is some background information about the situation, and about related discussions that we already had in this area:
This is a rather fundamental design choice. Extension methods are resolved at compile time, and this implies that the value of T
during the execution of extensionGetTType
is determined by the static type of the receiver. The receiver is test
, and the static type of test
is obtained from the initializing expression t as TestObject
, and TestObject
means TestObject<Object>
(it's a raw type, and the value of a raw type is obtained by a process called instantiation to bound, so the missing type argument is inferred to be the bound, which is Object
). This means that the returned value is Object
.
This means that it is not likely that Dart can accommodate a redesign of extension declarations such that they will use the dynamic value of type parameters: It's going to be a massively breaking change. Also, it will make extension methods less performant, and it may also eliminate some optimization opportunities.
However, we've had a number of discussions about doing things of this nature, provided via different mechanisms. For example #170 proposed a general mechanism which could be used to "look up" the value of a type argument (at compile time or at run time), and #177 proposed a variant of the extension feature which would allow for dynamic dispatch as well as dynamically bound type parameters.
from language.
It's maybe worth noting that if generics were resolved at runtime rather than compile time, then issues such as #620 could be resolved using extensions.
We would be able to do:
class Unary<A> {}
void doSomething<T extends Unary<Object?>>(T unary) {
unary._doSomething();
}
extension<A> on Unary<A> {
void _doSomething() {
print('use $A');
}
}
This wouldn't work for all cases though.
from language.
To be fair, relying on extensions for such a thing is probably undesirable. It would mainly work with functions do not use the generic in either return value or parameters.
So we probably can't avoid #620.
from language.
Related Issues (20)
- Late parameters, late-init-query operator, parameter element HOT 11
- More user-definable unary operators? HOT 4
- `switch` should understand that when a field of an object with a parameter type T that shares T with one of its fields and the field is exhaustively checked it may be promoted HOT 1
- `switch` should be considered exhaustive when an object of type S with a type parameter T shares T with one of its fields with type U, which is a sealed class, and has T checked against all possible types for U HOT 4
- Improved null-aware parsing HOT 3
- Augmentation of extensions seems to be difficult HOT 3
- What does `augment` mean when applied to an `enum` value? HOT 1
- Augmentations should specify allowed partial declarations.
- Incorrect difference.inDays value after time change HOT 2
- Extension types should implements record types HOT 1
- Change more elements of augmenting type declarations to be optional? HOT 1
- Improve inference of function literal parameter types HOT 5
- Inference of the declared type in a for-in statement differs from local variable declarations HOT 2
- Some questions about augmentation errors HOT 3
- Implicit coercion through implicit constructors.
- Propagating type parameter from generic function's argument to the function call HOT 2
- A more intentional and query-like API for macro source introspection HOT 1
- Make macro implementation code spearately compilable with its own version solve HOT 1
- Question: can augmentation define the rest of the method body? HOT 1
- 404 link in macro spec 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 language.