With Delphi XE4 for the iOS platform a new string type was introduced: Immutable zero based strings. So far Delphi had copy on write mutable strings. So the question is, what does that mean for my future programming? Are there any advantages of one string type over the other? What are the pitfalls I need to take care of when switching to the new string type (Other than the obvious 0 vs 1 base)?
相关问题
- Is there a Delphi 5 component that can handle .png
- Is there a way to install Delphi 2010 on Windows 2
- Is TWebBrowser dependant on IE version?
- iOS objective-c object: When to use release and wh
- DBGrid - How to set an individual background color
相关文章
- Best way to implement MVVM bindings (View <-> V
- Windows EventLog: How fast are operations with it?
- How to force Delphi compiler to display all hints
- Coloring cell background on firemonkey stringgrid
- HelpInsight documentation in Delphi 2007
- Can RTTI interrogate types from project code at de
- What specifically causes EPrivilege to be raised?
- Equivalent to designer guidelines in code
According to Marco Cantù's whitepaper, the
string
data type in the XE4 iOS target is not in fact immutable, although he seems to contradict himself.He says:
But he then goes on to say:
And also:
And he then shows a picture of an iOS device with mutating strings.
And in the official documentation we have:
So I interpret all that as meaning that the XE4 release of the iOS compiler still has mutable strings. The developers really don't want you to mutate your strings any more and are telling you that strings are immutable on the mobile compilers. But they do appear still to be mutable. Go figure!
However, you have been served notice that in a future release, the string may become immutable.
You can prepare for that future release now by setting
which will give you an idea of the impact of the change. If you want to buckle up and stop mutating strings, you can do this:
Once you do that you'll need to convert code that accesses individual string elements. I suspect you'll be surprised by how little such code there is. I just compiled 600,000 lines of code and saw only 120 instances of the warning. And most of those were in third party units. I've seen quite a stir about this change, but I honestly don't believe that very much code mutates strings. In the overwhelming majority of cases strings are built up by concatenation, or by calls to functions like
Format
. That code is not affected by this.I don't think there are any great pitfalls. You can use
{$WARN IMMUTABLE_STRINGS ...}
to let the compiler guide you through the process. Any code that mutates strings should be converted to useTStringBuilder
.As for the benefits of immutability, I refer you to Why .NET String is immutable?
If you are using the traditional Windows or OSX compilers then I see no compelling reason to change. The iOS compiler is brand new. The change to immutable strings has been floated, but it may never happen. It may happen only on the mobile compilers and never on the traditional compilers. Right now, I would sit tight, and wait to see how it all plays out.