Reflecting primitive types, such as "double", produces an unexpected result

The following example fails:

FAIL: MyClass tests getClassReturnsConstructorForDouble Expected: ?:<double> Actual: ?:<double> 

Example:

 test("getClassReturnsConstructorForDouble", () { double object = 10.1; Type objectClass = reflect(object).type.reflectedType; expect(objectClass, equals(object.runtimeType)); }); 

Is it impossible to accurately reflect int / double types?

+2
source share
2 answers

reflectType gets the true type of implementation. The runtimeType type may also be at its discretion.

There may be different implementations for numbers of various kinds of numbers, for strings and other built-in types, but they are displayed as well-known.

For example, we may have different types of implementations for integers of different sizes, but they will all say that their runtimeType is int. If you think about them, you will see the difference.

Another example might be String. There may be specialized classes for strings that are pure ASCII, for example, because they can be represented more compactly. This is not displayed at a basic level: runtimeType - String. You cannot detect it unless you dig it through reflection.

+6
source

I think reflection returns you the correct types, but your test fails because objectClass and object.runtimeType are two separate objects and therefore are not equal to each other. If you first convert them to a string, they will be equal to each other:

 expect(objectClass.toString(), equals(object.runtimeType.toString())); 
0
source

All Articles