I am using the operator overloading for records in Delphi 2006. (Please don't answer this question by telling me not to.)
I have two record types with the implicit operator overloaded. They are both only in the implementation of the module, not exposed through the interface.
My problem is, now that they are mutually dependent, I don't know how to forward declare the second type to the compiler. I know how to do this with functions, procedures, and classes, but not with records.
Here is a simplified example of what I am trying to do:
implementation
type
TMyRec1 = record
Field1 : Integer;
class operator Implicit(a: TMyRec2): TMyRec1; // <---- Undeclared Identifier here.
end;
TMyRec2 = record
Field2: Integer;
class operator Implicit(a: TMyRec1): TMyRec2;
end;
class operator TMyRec1.Implicit(a:TMyRec2): TMyRec1;
begin
Result.Field1 := a.Field2;
end;
class operator TMyRec2.Implicit(a:TMyRec2): TMyRec2;
begin
Result.Field2 := a.Field1;
end;
The Implicit Operator According to MSDN, an implicit keyword is used to declare an implicit user-defined type conversion operator. In other words, this gives the power to your C# class, which can accepts any reasonably convertible data type without type casting.
Use the operator and implicit or explicit keywords to define an implicit or explicit conversion, respectively. The type that defines a conversion must be either a source type or a target type of that conversion. A conversion between two user-defined types can be defined in either of the two types.
An implicit conversion from type S to type T is defined by an implicit value which has function type S => T , or by an implicit method convertible to a value of that type. Implicit conversions are applied in two situations: If an expression e is of type S , and S does not conform to the expression's expected type T .
An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.
You can't have forward declarations for record types. Define both Implicit
operators in the second type:
type
TMyRec1 = record
Field1 : Integer;
end;
TMyRec2 = record
Field2: Integer;
class operator Implicit(a: TMyRec2): TMyRec1;
class operator Implicit(a: TMyRec1): TMyRec2;
end;
Quoting from the help:
Implicit conversions should be provided only where absolutely necessary, and reflexivity should be avoided. It is best to let type B implicitly convert itself to type A, and let type A have no knowledge of type B (or vice versa).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With