by refulgentis · 2y ago · 0 comments
That's what if let y = x is for, it creates a safe lexical context to use the unwrapped value.
And then you have guard let y = x which is a stronger guarantee that no code further down in the context is executed if x is nil. This helps to write happy-path code with early returns, avoiding pyramids of doom and encouraging writing code where all the assumptions are listed up front with logic handling exceptional cases before finally arriving at the logic that works under all the assumptions.
Dart feels like a step backwards after seeing the benefits these provide.
But there’s also no harm in combining them. I agree with the OP, TypeScript does what they describe Dart doing and I find it much simpler than the way Swift does it. It’s never confusing.
Disagree. I’ve seen people write things like the following:
if x != nil { x!.foo() }
and then later the x != nil condition is changed to no longer check for nil, or the x!.foo() is moved out of that lexical scope elsewhere, or copypastad, and then the force unwrap crashed. I’ve seen similar yet more subtle bugs with soft unwraps instead of forced: x?.foo()Not possible if you use if let y = x . If you copypasta the inner scope with the different variable name then it won’t compile.
(ofc these are trivial representative examples, in reality these were lexical scopes with many lines and more complicated code)
It’s not just about how the code looks when you write it, it’s the paths that can be taken by future maintenance programmers.
I‘ve complained about the large surface area of swift syntax, stdlib etc in the past, but I don’t personally find this specific thing confusing.
For moving the force unwrapped out of the if, that's a problem with force unwrapping, not combining the syntaxes.
Dog? maybeDog;
if (maybeDog != null) {
maybeDog.bark();
}
If compiler says "sorry, maybeDog might be null", developer (rightfully so) usually responds "but I have just checked and I know it is not, why do you bother me?". So languages chose to accommodate this.> What if I want to set the value back to nil if it is not-nil?
You can. The type of the variable does not actually change. You could say that the information about more precise type is simply propagated to the uses which are guarded by the control flow. The following will compile just fine:
Dog? maybeDog;
if (maybeDog != null) {
maybeDog.bark();
maybeDog = null;
}
> Why should I have to wrap things back in an optional if I want to pass it along as such?You don't, with a few exceptions. There is a subtype relationship between T and T?: T <: T?, so the following is just fine:
void foo(Dog? maybeDog);
Dog? maybeDog;
if (maybeDog != null) {
maybeDog.bark();
foo(maybeDog); // Dog can be used where Dog? is expected
}
You might need to account for it in places where type inference infers type which is too precise, e.g. Dog? maybeDog;
if (maybeDog != null) {
// This will be List<Dog> rather than List<Dog?>.
final listOfDogs = [maybeDog];
}
Though I don't think it is that bad of a problem in practice.if (value == null) { // value is still nullable here }
instead you need to first capture the value
final value = this.value if (value == null) { // value is non-null }
Swift's guard and if let syntax seems way nicer here. In swift, it's also nice to destructure nested nullable values using if let.
if let value = object.object?.value { // value is non null }
In dart you'd need to first assign this into a local variable and then do the null check which is unnecessarily verbose.
if (value case final value?) {
// value is a fresh final variable
} if let value = value {
// a new binding named ‘value’ refers to value, unwrapped
}
or (newer syntax that I initially found even weirder than the older one, but both grew on me) if let value {
// a new binding named ‘value’ refers to value, unwrapped
}
(The old version will remain, as it is more flexible, allowing such things as if let baz = foo.bar(42) {
// a new binding named ‘baz’ refers to foo.bar(42), unwrapped
}
)