title: How do I test a generic type variable for equality with Default(T) in Delphi?
body: <p>I'm trying to write a generic cached property accessor like the following but am getting a compiler error when trying to check whether the storage variable already contains a value:</p>
  
  <pre><code>function TMyClass.GetProp&lt;T&gt;(var ADataValue: T; const ARetriever: TFunc&lt;T&gt;): T;
  begin
    if ADataValue = Default(T) then // &lt;-- compiler error on this line
      ADataValue := ARetriever();
    Result := ADataValue;
  end;
  </code></pre>
  
  <p>The error I'm getting is "E2015 Operator not applicable to this operand type".</p>
  
  <p>Would I have to put a constraint on <code>T</code> to make this work? The help file says that <code>Default()</code> would accept anything except generic types. In my case I'm dealing mostly with simple types like <code>String</code>, <code>Integer</code> and <code>TDateTime</code>.</p>
  
  <p>Or is there some other library function to perform this particular check?</p>
  
  <p>I'm using Delphi 2009 in case that matters.</p>
  
  <hr>
  
  <p>P.S.: Just in case it isn't clear from the code what I'm trying to do: In my case determining the actual property values might take a while for various reasons and sometimes I might not even need them at all. On the plus side however the values are constant so I only want to call the code that determines the actual value the first time that property is accessed and then store the value in a class field and the next time that property is accessed return the cached value directly. Here's an example of how I hoped I would be able to use that code:</p>
  
  <pre><code>type
    TMyClass = class
    private
      FSomeProp: String;
      function GetSomeProp: String;
  
      function GetProp&lt;T&gt;(var ADataValue: T; const ARetriever: TFunc&lt;T&gt;): T;
    public
      property SomeProp read GetSomeProp;
    end;
  
  function GetSomeProp: String;
  begin
    Result := GetProp&lt;String&gt;(FSomeProp,
                              function: String
                              begin
                                Result := SomeSlowOrExpensiveCalculation;
                              end);
  end;
  </code></pre>
  
  <p>(obviously, there's more than just one property)</p>