如何在DART中执行运行时类型检查?

DART规范规定:

具体化的类型信息反映了运行时的对象类型,并且可能始终由动态类型检查构造( InstanceOf、Cast、TypeCase等的类似物。在其他语言中).

听起来不错,但没有类似ABC0的__操作符。那么,如何在DART中执行运行时类型检查呢?这有可能吗?

190871 次浏览

The instanceof-operator is called is in Dart. The spec isn't exactly friendly to a casual reader, so the best description right now seems to be http://www.dartlang.org/articles/optional-types/.

Here's an example:

class Foo { }


main() {
var foo = new Foo();
if (foo is Foo) {
print("it's a foo!");
}
}

There are two operators for type testing: E is T tests for E an instance of type T while E is! T tests for E not an instance of type T.

Note that E is Object is always true, and null is T is always false unless T===Object.

As others have mentioned, Dart's is operator is the equivalent of Javascript's instanceof operator. However, I haven't found a direct analogue of the typeof operator in Dart.

Thankfully the dart:mirrors reflection API has recently been added to the SDK, and is now available for download in the latest Editor+SDK package. Here's a short demo:

import 'dart:mirrors';


getTypeName(dynamic obj) {
return reflect(obj).type.reflectedType.toString();
}


void main() {
var val = "\"Dart is dynamically typed (with optional type annotations.)\"";
if (val is String) {
print("The value is a String, but I needed "
"to check with an explicit condition.");
}
var typeName = getTypeName(val);
print("\nThe mirrored type of the value is $typeName.");
}

Dart Object type has a runtimeType instance member (source is from dart-sdk v1.14, don't know if it was available earlier)

class Object {
//...
external Type get runtimeType;
}

Usage:

Object o = 'foo';
assert(o.runtimeType == String);

Just to clarify a bit the difference between is and runtimeType. As someone said already (and this was tested with Dart V2+) the following code:

class Foo {
@override
Type get runtimeType => String;
}
main() {
var foo = Foo();
if (foo is Foo) {
print("it's a foo!");
}
print("type is ${foo.runtimeType}");
  

}

will output:

it's a foo!
type is String

Which is wrong. Now, I can't see the reason why one should do such a thing...

Simply use .runtimeType on the property like below,

print(unknownDataTypeProperty.runtimeType)

Exact type matching is done via runtimeType property. Checking if an instance or any of its parent types (in the inheritance chain) is of the given type is done via is operator:

class xxx {}


class yyy extends xxx {}


void main() {
var y = yyy();
  

print(y is xxx);
print(y.runtimeType == xxx);
}

Returns:

true
false

print("enter your phone number:\n"); var phone number = stdin.readLineSync();

if(phone number.runtimeType is int == true)     // checks if the values input are integers
{
print('you have successfully input your phone number!');
}
else{
print('only numbers are allowed');
}

T is The type

   print( T.runtimeType)

enter image description here


enter image description here

if(value is int ) Returns true if the type of the value is int, else if(value is! int )