Skip to content

Commit 607dc8b

Browse files
committed
basic: Add tryCast method
A small helper method for casting an object to a specific type, or returning `null` if that's not successfull. This will become handy in the following commits where we want to cast a `ZulipStream` object to a `Subscription` object.
1 parent 5a6b96a commit 607dc8b

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

lib/basic.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,6 @@ class OptionSome<T> extends Option<T> {
6666
@override
6767
String toString() => 'OptionSome($value)';
6868
}
69+
70+
/// Returns [object] as [T] if it matches the type, otherwise `null`.
71+
T? tryCast<T>(dynamic object) => object is T ? object : null;

0 commit comments

Comments
 (0)