I'm not an expert on Swift, but Dart will use null checks to automatically promote types, and I think that mostly does the job of if let
and guard let
.
For example:
String? x = possiblyReturnsNull();
if (x != null) {
// All code within this block treats `x` as non-nullable.
}
// All code outside the block continues to treat `x` as nullable.
Note that promotion won't be performed on non-local variables, so for those you would need to explicitly introduce a local reference. (There is a language proposal to provide a mechanism to allow a nicer mechanism to add a local reference without polluting the outer scope.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…