Solved

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

Posted on 2014-11-06
5
407 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
  • 2
  • 2
5 Comments
 
LVL 26

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RESTRequest Parameter 4 43
Multi-layered image in FireMonkey 9 49
Work with App store 7 68
scroll down TListBox component in Delphi 1 19
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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

828 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