Date -> DateTime gotcha

Consider this code:

var d as Date // nil
var dt as DateTime = d // Should also be nil ...
// ... but is UnsupportedOperationException

Unfortunately the compiler can’t catch this, and, AFAIK, there is no way to return nil from Operator_Convert.

I’d prefer they remove DateTime.Operator_Convert and replace it with DateTime.FromDate.

So the reason for this not working the way you expect is that for the Operator_Convert to work, it needs an Instance of DateTime because it’s an instance method.

The current behavior is better than a silent fail due to a human error forgetting a New operator.

This is incorrect, Operator_Convert will create an instance using the given parameter.

Edit: I see your clarification in Feedback that the instance is created behind the scenes, so it already exists by the time Operator_Convert runs.

This was a simple example for illustration. My actual use case looked like this:

var dt as DateTime = someClass.DateProp
if dt is nil then
  ...

This compiles and works most of the time, but raises that exception when someClass.DateProp is nil. If the code had been converted from Date to DateTime, this would silently introduce a bug where none existed before.

Couldn’t reproduce it with:

Public Function GetNewDateTime() as DateTime
  Return Nil
End Function


  Var dt As DateTime = GetNewDateTime()
  If dt is Nil Then
    MessageBox "Got a Nil"
  End
  Break

// On 2020r2.1

Replace the return parameter in the Function with Date. Operator_Convert doesn’t come into play with your code.

But that’s actually a good illustration of my point. If the result of the function is Nil, you’d expect dt to be Nil, and it shouldn’t matter if the function is returning a Date or DateTime.

2 Likes

This solves your problem:

Public Function ConvertDatesToDateTime(d As Object = Nil) as DateTime
  
  If d Isa Date Then Return New DateTime(Date(d))
  
  If d Isa DateTime Then Return DateTime(d)
  
  Return Nil
  
End Function



  Var dt As DateTime = ConvertDatesToDateTime(Nil) // Nil
  
  Break
  
  dt = ConvertDateToDateTime(New Date) // Will receive it converted
  
  break
  
  dt = ConvertDateToDateTime(DateTime.Now) // No problem either

  break

1 Like

Thanks for taking the time, but so does checking for Nil before the assignment. That’s not the point.

The point is that a DateTime assignment needs a DateTime object instantiated or nulled, receiving an improper object causes an exception at some given time, a conversion tool as I did solves the problem.

Yes, it does, but the point is about expectations. If I assign nil to a variable, I expect it to be nil, not raise an exception. Operator_Convert needs to have some way to make that so, or maybe should do that in the framework.

The number of things that would break if we changed the behavior of Operator_Convert is enormous. I just can’t imagine that we would do this.

Understood, but certainly an option could be added to give the developer a choice? Maybe an Operator_Convert_Strict variation?