DEV Community

Discussion on: On if let bindings in SwiftUI

Collapse
 
dimpiax profile image
Dmytro Pylypenko

if let – is from another paradigm.
The sense of SwiftUI goes to declarative approach, but not into imperative, which is classic.

Collapse
 
marionauta profile image
Mario Nachbaur

If you're saying that if let isn't supported in SwiftUI because it's not declarative, then why is the normal if supported? It's even used in the official documentation. To me, both share the same level of "imperativeness", but the if is not as type-safe in this case!

It's clear that SwiftUI it's inspired by other declarative frameworks like React.js or Vue.js, and in those is idiomatic to check if values are present to conditionally render a view. Something like this:

library.address !== undefined && <span>{library.address}</span>

Here, since we are checking that library.address has a value, TypeScript smart casts it from a string? to a normal string. It's the TS equivalent of an if let binding.

Collapse
 
dimpiax profile image
Dmytro Pylypenko

I wouldn't use React as example of correctness, as it's also not a mature technology.
In case of SwiftUI, I think, they will propose appropriate usage for cases that you aware of.