-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Consider adding ".classType" or "Type.classOf" #31459
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
/cc @rakudrama @sigmundch to sanity check my idea. |
It might be better to have a separate feature for each use-case. if (Type.sameClass(this, other)) ...
if (a is exactly _ConcreteA) ...
print('${Type.classNameOf(this)} ...'); The dart2js type representation is going to change a lot in the future and constructors will not map simply to types. |
Sure, those suggestions above make sense to me. |
Seems there are plans already
https://github.com/dart-lang/sdk/blob/master/docs/newsletter/lib/lib.md |
I'm not sure how That is, I don't see what feature is being requested, or if it's just In most cases, I would recommend just using bool operator ==(dynamic other) {
if (other is A) { // even promotes for you. Allows mocks!
// .. do checks ..
}
return false;
}
void useA(A a) {
if (a.runtimeType is! _ConcreteA) { // If you don't subclass the private class yourself, nobody will.
throw 'Do not subclass "A"';
}
} The Generics is a problem when you try to use runtime types for type comparisons (and the current solution is to just not do so). A way to get a non-generic representation of a generic class type is also interesting. |
Acknowledging the points that Stephen and others made already, we have had requests for a feature like That could be a new kind of instance of type In particular, it would be helpful for the package reflectable to be able to get the generic class of the class of a given instance, in order to create an appropriate instance mirror for it. Moreover, it was perceived as a clear improvement to be able to get the generic class rather than the plain So it might actually be helpful for the space (and maybe time) economy to have this type of feature. Returning to Matan's remark:
I'd prefer to say that the ability to get the One tricky issue is what to do when the generic class of an instance of a non-generic class is requested (say, a |
Simple. In JavaScript, every class has a function type: class Foo {}
function example() {
var foo = new Foo();
if (foo.constructor == Foo) {
// ...
}
} To "match" Dart semantics, we could use close to @rakudrama's suggestion: class Foo {}
void main() {
var foo = new Foo();
if (Type.isExactly(foo, Foo)) {
// ...
}
} No overhead.
Well, not everyone is (pun intended). See all of the Flutter examples above. There is (unfortunately) a lot of examples of this internally for web clients too. It seems like |
So you are saying that The examples used non-generic classes, and you seemed concerned that it doesn't handle the generic case, so it seemed like you wanted something that did reflect the runtime type, not the class of the runtime type. In that case I couldn't see any difference between that and |
There can be performance implications with runtimeType even when the user
only writes non-generic classes: if dart2js cannot prove that .runtimeType
is never called on a generic instance, it needs to store runtime type
information for all objects.
|
I absolutely believe that a "class reflection" is more efficient than a "type reflection" (and often more useful), I just wasn't sure what was actually asked for here. |
Today
runtimeType
is (mis?)used for the following scenarios:See usage in Flutter for some background.
Also see issues with
.runtimeType
in Dart2JS: #31329Ease of use in implementing
operator==
Examples
Checking that the type of an object is exactly something:
Examples:
Note that is check would not be necessary with #27372.
Outputting debug information about a class instance:
Examples
I believe most of these cases could be addressed without more expensive reified types by introducing a
.classType
orType.classOf
. In JavaScript this could simply map to the already present prototype:One of the cases it doesn't address is generic types, for example:
... but that might be reasonable, and allow better optimizations in most scenarios.
The text was updated successfully, but these errors were encountered: