gpt4 book ai didi

Delphi XE4 不可变字符串

转载 作者:行者123 更新时间:2023-12-03 14:36:00 25 4
gpt4 key购买 nike

iOS 平台的 Delphi XE4 引入了一种新的字符串类型:不可变的基于零的字符串。到目前为止,Delphi 已经实现了写可变字符串的复制。所以问题是,这对我 future 的编程意味着什么?一种字符串类型相对于另一种字符串类型有什么优点吗?切换到新的字符串类型时需要注意哪些陷阱(除了明显的 0 vs 1 基数)?

最佳答案

根据Marco Cantù's whitepaper ,XE4 iOS 目标中的 string 数据类型实际上并不是不可变的,尽管他似乎自相矛盾。

他说:

In the new Delphi LLVM-based compiler, there is one string type, representing Unicode strings (UTF16), and mapped to the current string type in Delphi XE3 (an alias for the UnicodeString type on the Windows compiler). However, this new string type uses a different memory management model. The string type is still reference counted, but it is immutable, which means you cannot modify the string contents once it is constructed.

但他接着说:

In other words strings are now Unicode-based, soon-to-become immutable, and reference-counted.

还有:

Where things start to change, however, is when you modify an existing string, not by replacing it with a new value (in which case you get a brand new string) but when you modify one of its elements, as shown in this line of code (and also in the previous section, where I introduced the topic):

Str1 [3] := 'x';

All Delphi compilers use a copy-on-write semantics: If the string you modify has more than one reference, it is first copied (adjusting the reference counts of the various strings involved as required) and later modified.

The new compiler does something very similar to the classic one. It implements a copy-on-write mechanism, unless there is a single reference to the string, in which case the string gets modified in place. As an example, consider the following code, which outputs the in-memory location of the actual string.

然后他展示了一张带有变异字符串的 iOS 设备的图片。

official documentation 中我们有:

Strings are immutable (constant), so you cannot index into a string as an array and manipulate the characters in a string. If you attempt to modify a string, the Delphi mobile compilers might emit the message W1068 Modifying strings in place may not be supported in the future (Delphi). You can specify whether the message x1068 is emitted as a warning or an error. In the Hints and Warnings page, set the warning "Modifying strings in-place...." to "true" or "error".

所以我将所有这些解释为 iOS 编译器的 XE4 版本仍然具有可变字符串。开发人员真的不希望您再改变字符串,并告诉您字符串在移动编译器上是不可变的。但它们似乎仍然是可变的。去算算吧!

<小时/>

但是,您已获悉,在未来的版本中,该字符串可能会变得不可变。

您现在可以通过设置来为 future 的版本做好准备

{$WARN IMMUTABLE_STRINGS WARN}

这将使您了解更改的影响。如果你想系好安全带并停止改变字符串,你可以这样做:

{$WARN IMMUTABLE_STRINGS ERROR}

完成此操作后,您将需要转换访问各个字符串元素的代码。我怀疑您会对这样的代码如此之少感到惊讶。我刚刚编译了 600,000 行代码,只看到了 120 个警告实例。其中大部分都在第三方单位。我已经看到有关此更改的相当大的轰动,但老实说,我不相信有太多代码会改变字符串。在绝大多数情况下,字符串是通过串联或调用 Format 等函数来构建的。该代码不受此影响。

我认为没有什么大的陷阱。您可以使用 {$WARN IMMUTABLE_STRINGS ...} 让编译器引导您完成该过程。任何改变字符串的代码都应转换为使用 TStringBuilder

至于不变性的好处,我推荐你引用Why .NET String is immutable?

如果您使用传统的 Windows 或 OSX 编译器,那么我认为没有令人信服的理由进行更改。 iOS 编译器是全新的。对不可变字符串的更改已经浮出水面,但可能永远不会发生。它可能只发生在移动编译器上,而不会发生在传统编译器上。现在,我会静观其变,静观其变。

关于Delphi XE4 不可变字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16212411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com