Solved

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

Posted on 2014-11-06
5
371 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 25

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now