Skip to content

Commit 9eae830

Browse files
authored
[ffigen] Update docs about ObjC runtime types (dart-lang#3152)
1 parent aebbcec commit 9eae830

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

‎pkgs/ffigen/doc/objc_runtime_types.md‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,22 @@ Just like in the pure Dart case, the Dart static type determines
2626
what methods are allowed to be invoked, but now the method
2727
implementation that is actually invoked at run time is determined
2828
by the *Objective-C* runtime type. In fact, the Dart runtime type is
29-
completely irrelevant when doing Objective-C interop.
29+
completely irrelevant when doing Objective-C interop. Moreover, the
30+
Dart wrapper around the Objective-C object is an [extension type](
31+
https://dart.dev/language/extension-types#type-considerations),
32+
which means the Dart runtime type will always be [`ObjCObject`](
33+
https://pub.dev/documentation/objective_c/latest/objective_c/ObjCObject-class.html).
3034

3135
Dart's `is` keyword checks the Dart runtime type. You shouldn't use
32-
this on Objective-C objects, because the Dart runtime type is irrelevant, and
33-
often won't match the Objective-C runtime type. Instead of `x is Foo`,
34-
use `Foo.isA(x)`.
36+
this on Objective-C objects, because the Dart runtime type is always
37+
`ObjCObject`. If `Foo` and `Bar` are unrelated `ObjCObject`s, and `x`
38+
a `Foo`, then `x is Bar` will be true, making these checks useless and
39+
misleading. Instead of `x is Foo`, use `Foo.isA(x)`, which calls into
40+
Objective-C to check the runtime type of the underlying object.
3541

3642
Dart's `as` keyword changes the static type of an object (and also
37-
checks its runtime type). You shouldn't use this on Objective-C objects,
38-
because the runtime type check may fail since the Dart runtime
39-
type often won't match the Objective-C runtime type. Instead of `x as Foo`,
40-
use `Foo.as(x)`.
43+
checks its runtime type). Since the Objective-C wrapper objects are
44+
extension types, this works, but is unsafe. The implicit `is` check
45+
that `as` performs is useless, for the reasons mentioned above.
46+
Instead of `x as Foo`, use `Foo.as(x)`, which internally checks
47+
`Foo.isA(x)`.

0 commit comments

Comments
 (0)