Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-11-06
5
Medium Priority
?
455 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 2000 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

Industry Leaders: 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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

688 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