Inline If common type of result

Just learned something again.
Method 1 below works and method 2 throws an exception.
That’s because of:

The return type is the common type between the two result values.
If - Xojo Documentation

But why is Date the common type between Date and DateTime?
Date is Deprecated.


Var d As DateTime
Var b As Boolean = False
Var dateResult As New Date

Try
  
  // Method 1
  
  If b = True Then
    
    d = dateResult
    
  Else
    
    d = Nil
    
  End If
  
  // Method 2
  
  d = If(b = True, dateResult, Nil)
  
Catch UnsupportedFormatException
  
  MessageBox "error"
  
End Try

Because the only object type on the right hand side is dateResult, which is a Date. The fact that d is a DateTime is irrelevant.

In other words, the type of the receiving variable isn’t considered in determining the “common type”. That is determined by the right hand side of the equals sign only.

One more reason why I dislike inline ifs …

Date is not DateTime so?
Make dateResult a datetime and your error is gone.

Autoconversion is not always worming as expected.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.