【问题标题】:What is System.String exactly in Delphi?Delphi 中的 System.String 到底是什么?
【发布时间】:2019-02-01 09:55:58
【问题描述】:

我维护了一些在特定单元中使用 EmptyString 的相当古老的 Delphi 代码。

编译器将其解析为

costant EmptyString = '' - 来自 System.string

现在我的硬盘中没有 System.string.pas 文件或 .dcu,因此我无法了解更多信息。

我发现

C:\Program Files (x86)\Embarcadero\Studio\17.0\source\rtl\sys\system.SysUtils.pas

另一个类似的常数:

{ Empty string and null string pointer. These constants are provided for
  backwards compatibility only.  }

  EmptyStr: string = '';
{$IFNDEF NEXTGEN}
  NullStr: PString = @EmptyStr;

乍一看,EmptyStrEmptyString 似乎只是在过去使用过,所以它们在某种程度上已弃用,因此最好直接使用 '' 或在应用程序中定义重新定义它们的常量.

现在升级第 3 方组件后,我意识到 EmptyString 不再解决。

我的问题是: System.String 到底是什么?为什么rtl中没有对应的.pas文件?

我用谷歌搜索没有成功。

谢谢。

【问题讨论】:

  • 我不记得读过System.string 但它可能只是Delphis 内置的string 类型。相关代码部分在 System.pas 单元中,部分内置在编译器中。
  • System.string type 字符串in unit System。它不是类或任何东西,它是内置类型,是编译器的特殊类型,由运行时管理。这篇文章描述得好一点(连同 PChar):"PChars: no strings attached"
  • @Uli:我有。您会在错误消息等(例如Incompatible types: 'System.string' and 'System.Integer' 或类似内容)和 RTTI 中看到它。

标签: delphi delphi-10-seattle


【解决方案1】:

System.StringStringSystem单元中正式定义的类型。

string 类型实际上是System.UnicodeString 类型的别名。文档说它被定义为:

type String = UnicodeString;

UnicodeString 的文档反过来说:

type UnicodeString = { built-in type };

没有定义类型的Pascal 代码,因为它是内置内在 类型。所有内置类型都被官方视为在 System 单元中声明,即使您在该单元的 Pascal 源代码中看不到它们的声明。

【讨论】:

  • 感谢您的解释。所以 EmptyString 在某种程度上是一个“隐藏常量”?不建议使用吗?
  • 不,EmptyString 未在 Delphi RTL 中定义。如果引用它的代码确实可以编译,那么您的代码必须定义EmptyString。另一方面,正如您所注意到的,EmptyStr 是在 System.SysUtils 单元中定义的。您的问题是关于 System.String 类型的。不要混淆类型、变量和常量。
  • 我已经解释过了。句号2 在我之前的评论中。
  • 是的,抱歉。现在我明白了。我意识到对于 Delphi 中的任何字符串,提示都会显示“System.String”!非常感谢,您澄清了我的疑问并帮助我了解更多关于 rtl 的信息。
猜你喜欢
  • 1970-01-01
  • 2021-02-15
  • 2013-05-29
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-06
  • 2013-05-05
相关资源
最近更新 更多