title: Type does not have null as a proper value
body: <p>For the sample program:</p>
  
  <pre><code>type public MyClass(reasonForLiving:string) =
      member x.ReasonForLiving with get() = reasonForLiving
  
  let classFactory () = MyClass("up to you")
  let live () =
      let instance = classFactory()
      if instance = null then raise(System.Exception("null is not living... that's why OO languages die from bugs"))
      instance
  </code></pre>
  
  <p>I get the error "The type 'MyClass' does not have null as a proper value" when I go to use this class as a return value of implicitly typed functions and compare it to <em>null</em> (b/c of compatibility requirements with C# dependency injection I cannot rely on F# option types).</p>
  
  <p>I can easily fix this by changing the null check to:</p>
  
  <pre><code>if instance :&gt; obj = null then
  </code></pre>
  
  <p>However, I know ("feel") this is completely "wrong". Especially when I consider how MyClass is a reference type that shouldn't need to be boxed (speaking from a C# background).</p>
  
  <p>I've read about "F# Value Restriction" and how it impacts type inference, but I can't seem to gleam how it applies to this scenario.</p>
  
  <p><strong>Q: Is there another way to do this?</strong></p>
  
  <p><em>Aside #1:</em> I found a simpler method of getting the error...</p>
  
  <pre><code>type public MyClass(reasonForLiving:string) =
      member x.ReasonForLiving with get() = reasonForLiving
  let nullMyClass : MyClass = null
  </code></pre>
  
  <p><em>Aside #2:</em> I did try System.Nullable without thinking... MyClass is a reference type and not a value type (struct) which Nullable&lt;_> requires. So, just reassures me that I REALLY am dealing with a reference type and leaves me wondering why an object cast suddenly makes this work.</p>
  
  <p><strong>Update:</strong> For anyone interested, I used this as one solution for Common Service Locator with the three functions below. Each service requested must support <em>null</em>, so if the service class is defined in F#, you need to add the <code>[&lt;AllowNullLiteral&gt;]</code>:</p>
  
  <pre><code>let private getServiceLocator () =
      try Some(Microsoft.Practices.ServiceLocation.ServiceLocator.Current)
      with | _ -&gt; None
  
  let private getService serviceFactory =
      let serviceLocator = getServiceLocator()
      let service = match serviceLocator with 
                    | None -&gt; serviceFactory()
                    | _ -&gt; 
                      match serviceLocator.Value.GetInstance&lt;'a&gt;() with
                      | null -&gt; serviceFactory()
                      | svc -&gt; svc
      match service with
      | null -&gt; None
      | _ -&gt; Some(service)
  
  let private getRequiredService serviceFactory =
      let service = getService serviceFactory
      match service with
      | None -&gt; raise(MissingServiceException(""))
      | _ -&gt; service.Value
  </code></pre>