News for hackers. Stuff that matters.
Re:Apple’s Use of Swift in iOS 13 by ctdonath on Thursday September 26, 2019 @05:17PM (#21085646)
That's why force-unwrap is so bad: the compiler is telling you there's a risk, at a time you can do something sensible about it, and force-unwrap puts solving the problem off to the worst possible scenario. My production user base can't afford "actionable crash reporting" as a debugging tool vs compiler warnings; even 0.01% of users experiencing it would get very expensive.
Re:Apple’s Use of Swift in iOS 13 by alkonaut on Thursday September 26, 2019 @05:59PM (#21086041)
If a thing can’t be null/nothing and there is nothing you can do it’s usually best to tear the world down. Otherwise in the best case you have a button that does nothing but in the worst case something bad happens. In the normal case the crash just moves. “Catching” programmer errors because a program that limps along looks better than one that crashes is a design decision I never understood.
Re:Apple’s Use of Swift in iOS 13 by swatcoder on Thursday September 26, 2019 @06:21PM (#21086203)
If "a thing can't be null/nothing", then you've made an error in making it an optional.

If a thing might be null/nothing but can't be used when it is, then you should write code that indicates your understanding of that case. Catching an error doesn't imply that your program should proceed, it communicates that you the programmer understood your system and anticipated its failure states. Proceeding or aborting is a secondary decision.

Re:Apple’s Use of Swift in iOS 13 by ken on Friday September 27, 2019 @09:08AM (#21091497)
> If "a thing can't be null/nothing", then you've made an error in making it an optional.

I don't disagree with this, but I can point out gobs of places in AppKit where Apple has arguably 'made an error' and I can't do anything about it. Or in third-party C libraries, which hold 99% of the functionality I ultimately need, and were not designed for having nice Swift programming interfaces.

I feel like everyone saying "never force unwrap!" must be writing pure-Swift standalone functions, with no dependencies, including the operating system. The API for writing items to the Mac clipboard still documents that it can throw exceptions, which are impossible to catch in Swift!

Re:Apple’s Use of Swift in iOS 13 by alkonaut on Friday September 27, 2019 @05:32AM (#21090005)
Exactly. And in many languages that is quite difficult to ensure at compile time. In java all object parameters are by definition optoinal. Same thing in C# (before C#8 brought non-nullable types).

Obviously in F# or Haskell or C#8 or Rust this isn't a problem. But what we are talking about is: how do you handle, at runtime, in a language such as C#7 or Java, an null parameter being passed to a method that must return a value based on its parameter, but was passed null? It's a choice between very bad options where the least bad is usually to throw an ArgumentNullException or similar. Because there is nothing better to do.

Re:Apple’s Use of Swift in iOS 13 by saagarjha on Friday September 27, 2019 @02:19AM (#21088992)
> If "a thing can't be null/nothing", then you've made an error in making it an optional.

There are cases where the programmer cannot express this to the compiler without a bottom type.

Re:Apple’s Use of Swift in iOS 13 by panic on Friday September 27, 2019 @03:48AM (#21089479)
It's not just that it looks better. It can give the person using the software a chance to save their data.
Re:Apple’s Use of Swift in iOS 13 by alkonaut on Friday September 27, 2019 @04:56AM (#21089821)
That one should always do of course (but perhaps not to the default place because there is every chance the data is already corrupted)
Re:Apple’s Use of Swift in iOS 13 by saagarjha on Thursday September 26, 2019 @05:25PM (#21085717)
Yes, and I'm usually happy the compiler is there to warn me. However, the compiler is not great at telling me why something might fail, which can make it difficult to provide decent error handling. And of course, there are certain places where the compiler just cannot know that a certain operation will not fail.
Re:Apple’s Use of Swift in iOS 13 by ctdonath on Thursday September 26, 2019 @05:37PM (#21085822)
That's why you use it only when you can prove it won't fail. I'm ok with that, and said so at the start.