Why can nil be returned from a non-optional function in this case?
<p>I am trying the below code in Playground and I don't quite understand why the compiler doesn't give an error when <code>bar</code>, which is <code>nil</code>, is returned from a non-optional function; and why it does when essentially <code>nil</code> is being compared to <code>nil</code> and gives an error message which seems not correct when claiming that <code>foobar</code> can never be <code>nil</code>.</p>

<p>EDIT:
Since <code>Optional</code> is also <code>Any</code>, why returning <code>nil</code> directly as in <code>foo2</code> is not allowed?</p>

<pre><code>func foo() -&gt; Any {
    let bar: String? = nil
    return bar
}

let foobar = foo()

if foobar == nil {

}

func foo2() -&gt; Any {
    return nil
}
</code></pre>

<p><a href="https://i.stack.imgur.com/so34g.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/so34g.png" alt="enter image description here"></a></p>