Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi (-XE) : casting to a record type with implicit conversion

I have a record type with methods, representing an specific hardware measurement type, read from the instrument as a string. The record contains implicit coversion to (and from) a string. If I cast a string as a record type, it seems to work, but is this safe? That is, does casting a string to a record with implicit string conversion call the implicit conversion as per assigning a temporary value?

var  a: MeasurementRecord;         // record type with implicit string conversion & decode methods
b: string;
c:double;
begin
b := Edit1.Text;              // Or any other string source 
a:=b;                         //Ok
a:= edit1.text;               //Ok
c:= a.returnQc;                 // returns measurement quality value

c:= MeasurementRecord(Edit1.text).returnQC;   //Avoiding local variable. This works, but is it correct useage?

end;
like image 402
HMcG Avatar asked Oct 05 '11 19:10

HMcG


1 Answers

Yes, this is perfectly safe. The code MeasurementRecord(Edit1.text) will create a MeasurementRecord record from the string Edit1.Text using your

class operator Implicit(S: string): MeasurementRecord

and then call the function returnQC in it. (However, if you also have a

class operator Explicit(S: string): MeasurementRecord

then this will be used instead since the cast is actually explicit.)

like image 148
Andreas Rejbrand Avatar answered Sep 30 '22 14:09

Andreas Rejbrand