title: Why can I assign null to an Option?
body: <p>The following are valid statements in Scala:</p>
  
  <pre><code>scala&gt; var x: Option[Int] = Some(3)
  x: Option[Int] = Some(3)
  
  scala&gt; var x: Option[Int] = None
  x: Option[Int] = None
  </code></pre>
  
  <p>The following is invalid:</p>
  
  <pre><code>scala&gt; var x: Option[Int] = 3
  &lt;console&gt;:7: error: type mismatch;
   found   : Int(3)
   required: Option[Int]
         var x: Option[Int] = 3
  </code></pre>
  
  <p>So far these examples make sense to me; a value of type Option[T] can be either of type Some[T] or None, so the compiler prevents you from assigning a value which is of neither type.</p>
  
  <p>However, the Scala compiler appears to accept this:</p>
  
  <pre><code>scala&gt; val x: Option[Int] = null
  x: Option[Int] = null
  </code></pre>
  
  <p>If I then try to do a pattern match on the option (e.g. as below), I'll get failures I didn't expect - why doesn't the compiler protect me from this by rejecting the assignment of null?</p>
  
  <pre><code>x match {
    case Some(y) =&gt; println("Number: ", y)
    case None =&gt; println("No number")
  }
  </code></pre>