Jump to content
emileverh

Simpel types

Recommended Posts

Hi guys!

I am using D12. I have lots of code for ID’s like this:

 

type TProductID = record

  ID : nativeint;

end;

 

What I want is this:

type TProductID = nativeint;

 

And yes I know the last declaration works! But when you make a call to a procedure and you mixed the params by mistake you got no error. Is there any helper, record attribute, compiler directive or so where I did not think of?!?!

 

procedure TForm6.FormCreate(Sender: TObject);
begin
  var tmpid: TProductID := 6;
  AddToStock(tmpid, 100);
  AddToStock(100, tmpid);  // I want a compiler warning or error here!!!!!!!
end;

procedure TForm6.AddToStock(pid: TProductID; cnt: nativeint);  // Both uses internally nativeint
begin
  ShowMessage(pid.ToString);
end;

 

Now I have lot's(!) of database code like this in my program....

 

var cid : TCustomerID;

var pid: TProductID;

pid.ID := qryGetStockProductID.AsInteger;

cid.ID := qryGetStocCustID.AsInteger;

 

What I want is:

pid:= qryGetStockProductID.AsInteger;

cid := qryGetStocCustID.AsInteger;

 

Share this post


Link to post
Posted (edited)

One option would be to add some operator overloading to TProductID:

type
  TProductID = record
    ID: nativeint;
  public
    class operator Explicit(A: TProductID): NativeInt; overload;
    class operator Implicit(A: NativeInt): TProductID; overload;
    class operator Implicit(A: TProductID): string; overload;
  end;

class operator TProductID.Explicit(A: TProductID): NativeInt;
begin
  Result := A.ID;
end;

class operator TProductID.Implicit(A: NativeInt): TProductID;
begin
  Result.ID := A;
end;

class operator TProductID.Implicit(A: TProductID): string;
begin
  Result := A.ID.ToString;
end;

This allows to write something like this:

var cid : TCustomerID;
var pid: TProductID;

pid := qryGetStockProductID.AsInteger;
cid := qryGetStocCustID.AsInteger;

---

procedure TForm6.AddToStock(pid: TProductID; cnt: nativeint);  // Both uses internally nativeint
begin
  ShowMessage(pid);
end;

Note that the operator to convert TProductID to NativeInt is Explicit instead of Implicit. That prohibits the use of TProductID as a NativeInt parameter and the compiler throws an error when the parameters to AddToStock are given in the wrong order.

Edited by Uwe Raabe
  • Like 2
  • Thanks 1

Share this post


Link to post

Note that the Implicit string operator can make you fall into the same parameter order trap as AddToStock is for NativeInt.

 

To avoid that, you may prefer to replace the operator with a dedicated ToString function, keeping the original call to ShowMessage:

type
  TProductID = record
    ID: nativeint;
  public
    class operator Explicit(A: TProductID): NativeInt; overload;
    class operator Implicit(A: NativeInt): TProductID; overload;
    function ToString: string;
  end;

class operator TProductID.Explicit(A: TProductID): NativeInt;
begin
  Result := A.ID;
end;

class operator TProductID.Implicit(A: NativeInt): TProductID;
begin
  Result.ID := A;
end;

function TProductID.ToString: string;
begin
  Result := ID.ToString;
end;

---

procedure TForm6.AddToStock(pid: TProductID; cnt: nativeint);  // Both uses internally nativeint
begin
  ShowMessage(pid.ToString);
end;

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×