3

I have a routine that fetches a TDateTime from a database. I fetch it like this in another routine:

var lCrmStartDate := self.GetCrmStartDate;

This results in lCrmStartDate being inferred as Extended, giving me all sorts of trouble.

I now fetch the date like so:

var lCrmStartDate : TDateTime := self.GetCrmStartDate;

and that works fine.

Can someone explain to me why the compiler infers Extended instead of TDateTime (which is the return type for GetCrmStartDate())?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Paul Sinnema
  • 2,534
  • 2
  • 20
  • 33
  • My guess is that it's related to "As a general rule, if the right hand expression type is an integral type and smaller than 32 bits, the variable will be declared as a 32-bit Integer. You can use an explicit type if you want a specific, smaller, numeric type." (from https://blogs.embarcadero.com/introducing-inline-variables-in-the-delphi-language/) and for floating point right-hand-sides, it's extended to... extended (TDateTime = Double). – HeartWare Mar 11 '22 at 13:11
  • By definition, `TDateTime = type Double`. Had it been `TDateTime = Double`, this behaviour would have been expected. However, the `type` part makes `TDateTime` a distinct type (which means that, for instance, it can have its own record helper), so one could definitely argue that the type of your variable should be `TDateTime`. In a 64-bit app, `Extended` is the same thing as `Double`, but this is not the case in a 32-bit app. Indeed, `var D := Now; ShowMessage(SizeOf(D).ToString);` yields 10 in a 32-bit app. – Andreas Rejbrand Mar 11 '22 at 13:11
  • Unfortunately, the official language documentation website is down, so I cannot see the official documentation for Delphi's type inference. Also, I suspect the documentation isn't detailed enough for us to decide if this behaviour is by design or a bug or something in between. – Andreas Rejbrand Mar 11 '22 at 13:14
  • @HeartWare: Try `type TShoeSize = type Word; function GetShoeSize: TShoeSize;` It will make `var S := GetShoeSize` a `TShoeSize`. – Andreas Rejbrand Mar 11 '22 at 13:16
  • (My gut feeling is that this behaviour is bad. Your variable should be inferred to be of type `TDateTime`.) – Andreas Rejbrand Mar 11 '22 at 13:21
  • @AndreasRejbrand: You're right (which is in contradiction to the blog entry). But var S := GetShoeSize + 0; makes it a 32-bit integer :-) – HeartWare Mar 11 '22 at 13:22
  • @AndreasRejbrand: I agree, that it should be inferred to be a TDateTime value (ie. a Double, 8 bytes floating point). – HeartWare Mar 11 '22 at 13:22
  • 4
    Know bug: https://quality.embarcadero.com/browse/RSP-25799 – Uwe Raabe Mar 11 '22 at 13:42
  • Ah, a bug. I'm also a .Net developer and so used to the language being super solid that I never even thought of that possibility. The bug is open since 2019, not much happening there. – Paul Sinnema Mar 11 '22 at 13:48
  • Thanks people for the reactions. I just have to be very careful using dates in Delphi. This one hunted me for a while, now its clear. I upvoted the issue at Embarcadero for what its worth. – Paul Sinnema Mar 11 '22 at 14:24
  • I think you have to be careful with inline variables, not necessarily dates. Inline variables have some headaches that will eventually bite you. – Darian Miller Mar 11 '22 at 16:49
  • This is a terrible practice. Why introduce inline variables and then not properly implement them. This makes the Language weaker. Stop doing that or implement it properly. – Paul Sinnema Mar 11 '22 at 19:54
  • @PaulSinnema: IIRC, inline variable declarations were introduced in Delphi 10.3, but in Delphi 10.3, the IDE didn't understand them, so if you dared to make an inline variable declaration, it would get a wiggly red underline and the IDE would no longer understand your code fully; it would consider it syntactically invalid. Compared to that situation, the current situation is actually pretty decent. – Andreas Rejbrand Mar 11 '22 at 20:59
  • @PaulSinnema Much of Delphi is rock solid as it's been around for a long time and classic Win32 development is typically stellar. Many devs love inline variables and use them in all new development. Most compiler-related issues are resolved but be aware that the IDE can lag behind. Some issues: https://quality.embarcadero.com/browse/RSP-32507 https://quality.embarcadero.com/browse/RSP-33176 https://quality.embarcadero.com/browse/RSP-33365 https://quality.embarcadero.com/browse/RSP-22089 – Darian Miller Mar 11 '22 at 21:19
  • @DarianMiller: I totally understand you defending Delphi. I have programmed in Delphi a lot in my early days when it was still Borland. Recently I've reentered the realm. But the problem I posted is totally counter intuitive and should be resolved as soon as possible. Not wait 3 years and do nothing. Bad practice. – Paul Sinnema Mar 11 '22 at 21:46
  • @PaulSinnema I'm a Delphi MVP - I defend Delphi as a hobby. There's a new release coming out very soon, there is always the chance that it's fixed in a new version. I added my vote to the Quality Portal issue. We need to get more people to vote on it. https://blogs.embarcadero.com/whats-coming-in-rad-studio-join-the-upcoming-webinar/ – Darian Miller Mar 11 '22 at 22:25

0 Answers0