News for hackers. Stuff that matters.
Re:Embedded Swift on the Raspberry Pi Pico by teho on Wednesday March 06, 2024 @01:36AM (#39612987)
In Dart if you want to narrow down a nullable final class variable, it is not enough to do

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.

Re:Embedded Swift on the Raspberry Pi Pico by mraleph on Wednesday March 06, 2024 @02:54AM (#39613427)
In Dart 3 instead of declaring the variable and then comparing you can simply write an if-case pattern:

    if (value case final value?) {
      // value is a fresh final variable 
    }
Re:Embedded Swift on the Raspberry Pi Pico by teho on Wednesday March 06, 2024 @03:51AM (#39613773)
The more you know, I had totally missed this.
Re:Embedded Swift on the Raspberry Pi Pico by Someone on Wednesday March 06, 2024 @07:53AM (#39615408)
So Dart is moving in the Swift direction, which has

    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 
    }
)