简体   繁体   English

Delphi记录助手记录

[英]Delphi record helper for record

I have this code: 我有这个代码:

type
 TSolution = record
  x1, x2, i1, i2: double;
 end;

 TSupport = record helper for TSolution 
  function toFraction: string;
 end;

And I would like to be able to call something like this inside (for example) a button click event: 我希望能够在内部(例如)按钮点击事件中调用类似的内容:

var k: TSolution;
begin

 //...
 Memo1.Lines.Add(k.x1.toFraction);
 //...

end;

Where toFraction is a function I have written that converts a double into a fraction. toFraction是我编写的一个函数,它将double转换为一个分数。 I think that a record helper is what I need but why doesn't this work? 我认为我需要一个记录助手,但为什么这不起作用?

{ TSupport }

function TSupport.toFraction: string;
begin
 Result := TFunction.DoubleToFraction(self);
end;

If you are wondering, TFunction is a class (in the same unit as the record/record helper) and it has the DoubleToFraction class function which returns a string of course. 如果您想知道, TFunction是一个类(与记录/记录助手在同一单元中),它具有DoubleToFraction类函数,该函数返回一个字符串。

E2010 Incompatible types: 'Double' and 'TSolution' E2010不兼容类型:'双'和'TSolution'

Ok this is the error but if I don't use the self then how can I take the parameter? 好的,这是错误,但如果我不使用自我,那么我该如何获取参数? The parameter would be x1, x2, i1 or i2 depending on what I need. 参数将是x1,x2,i1或i2,具体取决于我的需要。 In the example above, inside Memo1 I should convert x1 (parameter) to fraction. 在上面的例子中,在Memo1里面我应该将x1(参数)转换为分数。 What to do here? 该怎么办?


I have noticed that if you write for example 我注意到,如果你写的例子

TSupport = record helper for double

Then the Self works! 然后自我工作! Basically I would like to know how to avoid the Self to refer to the TSolution record but to its double fields. 基本上我想知道如何避免自我引用TSolution记录但是它的双重字段。

Here is the wrong context: you call 这是错误的背景:你打电话

k.x1.toFraction

that means that you call a function toFraction that corresponds to x1 , but it is a double , not TSolution . 这意味着你调用一个与x1相对应的函数toFraction ,但它是一个double ,而不是TSolution

Your helper function will be called form something like 您的辅助函数将被称为类似的函数

k.toFraction

I assume that toFraction knows in advance which of the variables x1, x2, i1, i2: double; 我假设toFraction提前知道哪个变量x1, x2, i1, i2: double; to take. 采取。 Otherwise you can write a helper for double. 否则你可以为double写一个帮助器。

APENDED on @Remy Lebeau consideration APENDED上@Remy勒博考虑

You can use a construction like this: 你可以使用这样的结构:

TMyDouble = record
  private
    class var FX: double;
  public
    class property X: double read FX write FX;
    class operator Implicit(a: TMyDouble): double;
    class operator Implicit(a: double): TMyDouble;
    function toFraction: string;
  end;

function TMyDouble.toFraction: string;
begin
  result := FloatToStr(self.FX { .Fraction } ); // or other FX.methods
end;

class operator TMyDouble.Implicit(a: TMyDouble): double;
begin
  result := FX
end;

class operator TMyDouble.Implicit(a: double): TMyDouble;
begin
  result.FX := a;
end;

It will emulate double functionality and seem a double being a record. 它将模仿double功能,并且似乎是双重记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM