Reflecting primitive types such as “double” results in unexpected output

后端 未结 2 1985
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-22 10:36

The following example fails with:

FAIL: MyClass tests getClassReturnsConstructorForDouble
  Expected: ?:
    Actual: ?:
相关标签:
2条回答
  • 2021-01-22 11:15

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

    expect(objectClass.toString(), equals(object.runtimeType.toString()));
    
    0 讨论(0)
  • 2021-01-22 11:23

    reflectedType gets you the true implementation type. The runtimeType can lie, and does, by design.

    There can be varying implementations for numbers of various sorts of numbers, for strings and other built in types, but the they are exposed as the publicly known ones.

    For example, we might have different implementation types for integers of varying size, but they will all say their runtimeType is int. If you reflect on them, you can 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 exposed at the base level: the runtimeType is String. You cannot detect this unless you dig for it via reflection.

    0 讨论(0)
提交回复
热议问题