分配“增强记录”时我应该重载什么运算符?到正常的“数据类型”多变的?
首先,我需要知道我想做的事情是否可行。如果可能的话,我需要知道如何做。
演示问题比解释问题要容易得多,所以这里是:
我有一个“增强记录”(目的 - 虽然对这个问题并不重要 - 是生成一个“智能字符串”类型,以替换普通的字符串类型) :
TLKString = record
Value: String;
// Some methods here to operate on and build String values
// Allows me to assign String values directly to "instances"
// of this record type! I have others (hence "overload") to
// handle other data types (such as Integer etc.)
class operator Implicit(const AValue: String): TLKString; overload;
end;
我现在可以使用这个 TLKString 类型,如下所示:
var
LSmartString: TLKString;
begin
LSmartString := 'Hello World'; // The "Implicit" operator then
// assigns this to LSmartString.Value
end;
好的,到目前为止一切都很好!现在我们遇到问题了...
我需要能够将 LSmartString (TLKString 的“实例”)的值分配给普通的 String 变量...
var
LSmartString: TLKString;
LNormalString: String;
begin
LSmartString := 'Hello World';
// The next line works fine, but is not what I want!
LNormalString := LSmartString.Value;
LNormalString := LSmartString; // E2010 (Incompatible Types)
end;
这就是我遇到困难的地方,因为(我确信你'会注意到),上述片段的最后一行导致 E2010 不兼容类型:'string' 和 'TLKString'。当然,我知道情况会是这样……我不知道是否可以通过在 TLKString 记录类型上重载运算符来克服这个问题,如果可以,我需要重载哪个运算符来完成它。
如果这是不可能的,那么让我觉得 CodeGear 和 Embarcadero 使用 Implicit
和 Explicit
运算符来处理对增强的 Record 类型的值分配有点愚蠢,但没有运算符可以处理逆运算。
I need to know, first and foremost, if what I'm trying to do is even possible. If it is possible, I then need to know how.
It's far easier to demonstrate the problem rather than explain it so here goes:
I have an "Enhanced Record" (the purpose - though not important to this question - is to produce a "Smart String" type, to replace the normal String type):
TLKString = record
Value: String;
// Some methods here to operate on and build String values
// Allows me to assign String values directly to "instances"
// of this record type! I have others (hence "overload") to
// handle other data types (such as Integer etc.)
class operator Implicit(const AValue: String): TLKString; overload;
end;
I can now use this TLKString type as follows:
var
LSmartString: TLKString;
begin
LSmartString := 'Hello World'; // The "Implicit" operator then
// assigns this to LSmartString.Value
end;
Okay, so far everything is great! Now we get to the problem...
I need to be able to assign the value of LSmartString (an "instance" of TLKString) to a normal String variable...
var
LSmartString: TLKString;
LNormalString: String;
begin
LSmartString := 'Hello World';
// The next line works fine, but is not what I want!
LNormalString := LSmartString.Value;
LNormalString := LSmartString; // E2010 (Incompatible Types)
end;
This is where I come unstuck, as (I'm sure you'll notice), the last line of the above snip results in E2010 Incompatible types: 'string' and 'TLKString'. I, of course, knew this would be the case... what I don't know if it's possible to overcome by overloading an operator on my TLKString record type, and if so, what operator I need to overload to do it.
If this isn't possible, then it strikes me as a little silly of CodeGear and Embarcadero to have Implicit
and Explicit
operators to handle assignment of values to an enhanced Record type, but no operator to handle the inverse.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我自己已经回答了这个问题……这就是我所说的“显而易见”。
这是解决方案(为了他人的利益)
Okay, I've answered this myself... and it was what I would call "blindingly obvious".
Here's the solution (for the benefit of others)