The Dark Path
The main argument here apparently is &quot;lets not add safety features because we have a limited pool of features before the language becomes too complex&quot;. I don&#x27;t think thats a convincing argument. Its a question of which features you value. For example, I&#x27;d totally give up `public, private and protected` to be able to get rid of exception and `null` pointer problems forever.<p>&gt; It is programmers who create defects – not languages.<p>In the case of nulls in typed languages, its the language&#x27;s fault. The language is literally lying to you.<p>Lets say that Java is claiming, for example, that a method always returns a `string`. What is a string? Its a value of a certain class, that supports certain methods that return other values. In structurally typed (&quot;duck typed&quot;) languages, the type can be defined by the methods you can call on the value (the interface it supports)<p>But this claim is just not true. The method may return null, and if you call the string methods on that value, your code may die a violent death. Its clear that the value `null` doesn&#x27;t belong to the type `string`, since it doesn&#x27;t support the contract that a `string` provides. So why is the programmers fault to expect that the type system doesn&#x27;t lie to them? I think the programmer has the right to expect an actual string - not a number, not an `StringLike`, and definitely not null, all of which don&#x27;t fully satisfy the `string` contract.<p>Unfortunately, the language doesn&#x27;t give you the ability to discern between a string that cannot be null and a string-or-null (could be string, but also could be null). Because of this, you need to track all the places that might be null in your head to make sure you check for them, or look up the test code to remind yourself all the time.<p>Its a similar problem with exceptions. The language is claiming that functions that don&#x27;t cause exceptions act the same way as those that do. But thats painfully untrue, and you would find that out the moment you try to `fopen` a file, read something from it, and then `fclose` it.<p><pre><code>  f = fopen(&#x27;file.txt&#x27;)
  data = fread(f)
  fclose(f)

</code></pre>
Oops but the `read` call may throw, and you now have a dangling file handle. Good thing you knew that about `fread`! Now how about the other thousand methods in your code? Do you know whether any of those throw?<p>Whats that? You should be using `try-with-resources`, I hear? Okay, then what do I do with this code:<p><pre><code>  atomicIncrement(inFlightRequests)
  result = doRequest()
  atomicDecrement(inFlightRequests)
</code></pre>
I should use try-finally. Thats true, but what if doRequest() doesn&#x27;t throw? Then I don&#x27;t need to. Why do those two look the same? Why is the language lying that they are the same?<p>Now lets see whats available in Swift:<p>* `try!` means - I don&#x27;t expect this piece of code to throw, even though it &quot;might&quot;. If it does, crash horribly.<p>* `try` means - I know that this piece of code throws, but I can&#x27;t handle that exception here, so propagate it.<p>* no keyword - This code should not throw. If it returns a Result that may be an error, I want to handle it right here.<p>* `try?` similar as above, but discards the specific error and returns an Optional.<p>Thats it. You manage the risk, the language only makes that explicit. It also makes it really clear whats going on during code review.<p>Here is how the problematic piece of code looks when `doRequest` might throw:<p><pre><code>  atomicIncrement(inFlightRequests)
  result = try doRequest()
  atomicDecrement(inFlightRequests)

</code></pre>
The possible bug is now very easy to spot. We don&#x27;t need to remember whether `doRequest` throws or to look that up. Its clear that a finally or defer block is necessary here.<p>Whats even better is how the code looks like when `doRequest` doesn&#x27;t throw!<p><pre><code>  atomicIncrement(inFlightRequests)
  result = doRequest()
  atomicDecrement(inFlightRequests)
</code></pre>
Since we know the compiler would force a `try` keyword there if there was the possibility of exception, we know this code is correct just by glancing it. No need for `defer` or `finally`.<p>So are these two features not worth the cost? Maybe, if you are arguing that we don&#x27;t need types as a tool to prevent errors. In that case, it would indeed be the programmer&#x27;s fault for not writing a test for it.<p>But if you are going to have types, you might as well have types that tell the truth. Otherwise they&#x27;re useless.<p>Finally, IMO this is not a good reason to hate on types. Stop complaining that the compiler wont accept your buggy code and take the time to do the changes. I&#x27;m happy when the compiler points out all the places that will be affected by the change - thats something that unit tests with mocks will <i>never</i> catch!<p>Imagine that a change in doRequest caused it to throw, where previously it did not. The compiler will now tell you about ALL places that may be affected. What would happen if it didn&#x27;t? The `inFlightRequests` counter will suddenly start behaving strange and increasing indefinitely. What would happen if above a certain value new requests don&#x27;t get queued? A bug that leads to denial of service.<p>Is it worth the hassle of changing all affected code? I don&#x27;t know, what do you think?<p>There are much better complaints against type systems, like the fact that most are not able to understand certain advanced styles of metaprogramming yet. Or that it doesn&#x27;t do control flow analysis, e.g. if I type<p><pre><code>  if (x != null) { doCode(x); }
</code></pre>
then `x` should have its nullability removed in the block<p>or<p><pre><code>  if (x == null) { x = nonNullValue; }
</code></pre>
then below this line, x should not be considered nulable. TypeScript for example does both of the above.