| ▲ | _flux 5 hours ago | |||||||||||||||||||||||||
I personally don't enjoy the MyObject? typing, because it leads to edge cases where you'd like to have MyObject??, but it's indistinguishable from MyObject?. E.g. if you have a list finding function that returns X?, then if you give it a list of MyObject?, you don't know if you found a null element or if you found nothing. It's still obviously way better than having all object types include the null value. | ||||||||||||||||||||||||||
| ▲ | gm678 2 minutes ago | parent | next [-] | |||||||||||||||||||||||||
Different language, but I find this Kotlin RFC proposing union types has a nice canonical example (https://youtrack.jetbrains.com/projects/KT/issues/KT-68296/U...)
A proper option type like Swift's or Rust's cleans up this function nicely. | ||||||||||||||||||||||||||
| ▲ | ecedeno 3 hours ago | parent | prev | next [-] | |||||||||||||||||||||||||
Your example produces very distinguishable results. e.g. if Array.first finds a nil value it returns Optional<Type?>.some(.none), and if it doesn't find any value it returns Optional<Type?>.none The two are not equal, and only the second one evaluates to true when compared to a naked nil. | ||||||||||||||||||||||||||
| ||||||||||||||||||||||||||
| ▲ | lock1 3 hours ago | parent | prev | next [-] | |||||||||||||||||||||||||
Well, in a language with nullable reference types, you could use something like
to express what you want.But exactly like Go's error handling via (fake) unnamed tuple, it's very much error-prone (and return value might contain absurd values like `(someInstanceOfT, false)`). So yeah, I also prefer language w/ ADT which solves it via sum-type rather than being stuck with product-type forever. | ||||||||||||||||||||||||||
| ||||||||||||||||||||||||||
| ▲ | 2 hours ago | parent | prev | next [-] | |||||||||||||||||||||||||
| [deleted] | ||||||||||||||||||||||||||
| ▲ | skydhash 2 hours ago | parent | prev [-] | |||||||||||||||||||||||||
I like go’s approach on having default value, which for struct is nil. I don’t think I’ve ever cared between null result and no result, as they’re semantically the same thing (what I’m looking for doesn’t exist) | ||||||||||||||||||||||||||
| ||||||||||||||||||||||||||