Solved

How do I make a Delphi primary type equal to TDatetime, but not automatically castable to TDatetime

Posted on 2014-11-06
5
424 Views
Last Modified: 2014-11-13
I have the following example unit. How do I make TSinceDateTime so that it will not compile the code inside "ErrorConvert" and successfully compile the code in "SuccessConvert" ?  Basically, I want to have a TDateTime type that is a primary type similar to TDateTime in that it can hold a timestamp, but I want the compiler to check that I don't mix those two in my source code. I would like to avoid making TSinceDateTime as a class, because then I have to instantiate that in all my already existing code.

unit MyUnit;

interface

uses SysUtils, Windows;

type
  TSinceDateTime = type Double;

function ErrorConvert(sdt : TSinceDateTime) : TDateTime;
function SuccessConvert(sdt : TSinceDateTime) : TDateTime;

implementation

function ErrorConvert(sdt : TSinceDateTime) : TDateTime;
begin
  Result := sdt; // Should not compile
end;

function SuccessConvert(sdt : TSinceDateTime) : TDateTime;
begin
  Result := TDateTime(sdt); // Should compile
end;

end.

Open in new window

0
Comment
Question by:lefodnes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 40427903
So, you want that your code be compiled or not? Or you want to check if value sdt is real TDateTime?
not just plain Double? Variants can help in this....
0
 

Author Comment

by:lefodnes
ID: 40439779
I've requested that this question be deleted for the following reason:

Sorry. Bad explanation from my side, and now I found a better solution to my problem, which has nothing to do with this post.
0
 
LVL 19

Accepted Solution

by:
Thommy earned 500 total points
ID: 40439663
My below code does exactly what you want, but I'm not sure, if it makes any sense!!!
If you remove the comment symbols/double-slash from line " //Result := sdt; // Should not compile", the compiler throws the error:
E2010 Incompatible Types: 'TDateTime' and 'TSinceDateTime'

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

  TSinceDateTime = record
    SinceDateTime: TDateTime;
  end;

var
  Form1: TForm1;

function ErrorConvert(sdt : TSinceDateTime) : TDateTime;
function SuccessConvert(sdt : TSinceDateTime) : TDateTime;


implementation

{$R *.dfm}

function ErrorConvert(sdt : TSinceDateTime) : TDateTime;
begin
   //Result := sdt; // Should not compile
end;

function SuccessConvert(sdt : TSinceDateTime) : TDateTime;
begin
  Result := TDateTime(sdt); // Should compile
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  sdtRec: TSinceDateTime;
begin
  ShowMessage(DateToStr(now));
  with sdtRec do SinceDateTime:=Now;
  ShowMessage(DateToStr(SuccessConvert(sdtRec)));
end;

end.

Open in new window

0
 
LVL 19

Expert Comment

by:Thommy
ID: 40439699
Additionally I'd like to provide you some considerations about turning off implicit type conversions and if it makes sense or not...

How to turn off implicit type conversions?
Designing code by contract
0
 

Author Closing Comment

by:lefodnes
ID: 40439780
Ahh!! That's clever :) Thanks a lot. Easy solution, still clever.
So now I'm able to just change the type of all those TDateTime's that I have, to TSinceDateTime, and I will get a warning on those parts of the application that still tries to use them as a TDateTime. I will also look into that link you provided. Thanks again.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wincontrol not (correctly) drawn 15 59
Delphi: barcode reading on android platform 1 100
Broadcast a message using ICS 2 50
Firemonkey Listview item popup menu ? 1 74
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question