<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://www.eiffelroom.com" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>eiffelroom - Read-only strings - Comments</title>
 <link>http://www.eiffelroom.com/blog/colin_adams/read_only_strings</link>
 <description>Comments for &quot;Read-only strings&quot;</description>
 <language>en</language>
<item>
 <title>Done...</title>
 <link>http://www.eiffelroom.com/blog/colin_adams/read_only_strings#comment-207</link>
 <description>&lt;p&gt;Wiki document on &lt;a href=&quot;http://eiffelsoftware.origo.ethz.ch/index.php/Immutable_Strings&quot;&gt;Immutable Strings&lt;/a&gt;.&lt;/p&gt;

</description>
 <pubDate>Wed, 18 Apr 2007 10:42:21 -0700</pubDate>
 <dc:creator>paulbates</dc:creator>
 <guid isPermaLink="false">comment 207 at http://www.eiffelroom.com</guid>
</item>
<item>
 <title>I think the signature of</title>
 <link>http://www.eiffelroom.com/blog/colin_adams/read_only_strings#comment-205</link>
 <description>&lt;p&gt;I think the signature of is_equal is subject to change anyway.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;geshifilter eiffel&quot;&gt;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;like&lt;/span&gt; &lt;span style=&quot;color: #800080;&quot;&gt;Current&lt;/span&gt;&lt;/code&gt; will be replaced with &lt;code class=&quot;geshifilter eiffel&quot;&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+ANY&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;ANY&lt;/span&gt;&lt;/a&gt;&lt;/code&gt; IIRC.&lt;/p&gt;

&lt;p&gt;-- mTn-_-|&lt;/p&gt;

</description>
 <pubDate>Tue, 17 Apr 2007 09:36:44 -0700</pubDate>
 <dc:creator>mtn</dc:creator>
 <guid isPermaLink="false">comment 205 at http://www.eiffelroom.com</guid>
</item>
<item>
 <title>Taking the discussion elsewhere.</title>
 <link>http://www.eiffelroom.com/blog/colin_adams/read_only_strings#comment-204</link>
 <description>&lt;p&gt;I&#039;m in the process of creating a Wiki page with rationale and implementation suggestions. I&#039;ll post a link when it in at least a legible draft state.&lt;/p&gt;

</description>
 <pubDate>Tue, 17 Apr 2007 09:33:54 -0700</pubDate>
 <dc:creator>paulbates</dc:creator>
 <guid isPermaLink="false">comment 204 at http://www.eiffelroom.com</guid>
</item>
<item>
 <title>Comparison</title>
 <link>http://www.eiffelroom.com/blog/colin_adams/read_only_strings#comment-203</link>
 <description>&lt;p&gt;I think &lt;code class=&quot;geshifilter eiffel&quot;&gt;READABLE_STRING&lt;/code&gt; is better than &lt;code class=&quot;geshifilter eiffel&quot;&gt;READONLY_STRING_GENERAL&lt;/code&gt; (because the dynamic type may also be writable, and I think we will not need _8 and _32 descendants for this class).&lt;/p&gt;

&lt;p&gt;For comparison, there is &lt;code class=&quot;geshifilter eiffel&quot;&gt;same_string&lt;/code&gt; and &lt;code class=&quot;geshifilter eiffel&quot;&gt;is_equal&lt;/code&gt;. &lt;code class=&quot;geshifilter eiffel&quot;&gt;Same_string&lt;/code&gt; can accept a &lt;code class=&quot;geshifilter eiffel&quot;&gt;READABLE_STRING&lt;/code&gt;, and so avoid a conversion, but what about &lt;code class=&quot;geshifilter eiffel&quot;&gt;is_equal&lt;/code&gt;? It takes a &lt;code class=&quot;geshifilter eiffel&quot;&gt;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;like&lt;/span&gt; &lt;span style=&quot;color: #800080;&quot;&gt;Current&lt;/span&gt;&lt;/code&gt; argument, and so a conversion will be involved. Colin Adams&lt;/p&gt;

</description>
 <pubDate>Tue, 17 Apr 2007 01:51:22 -0700</pubDate>
 <dc:creator>colin-adams</dc:creator>
 <guid isPermaLink="false">comment 203 at http://www.eiffelroom.com</guid>
</item>
<item>
 <title>Inherit and convert</title>
 <link>http://www.eiffelroom.com/blog/colin_adams/read_only_strings#comment-202</link>
 <description>&lt;p&gt;Can&#039;t we have both 2) and 3)? Mutable strings would conform to constant strings and constant strings would convert to mutable strings. Comparison would use constant strings as argument and hence conformance would be involved (no performance implications). The problem with STRING_GENERAL inheriting from CONSTANT_STRING_GENERAL is that even though it&#039;s a good way for a feature to state that it won&#039;t alter strings passed as arguments if declared as constant, it does not mean that this string will not be modified by another feature (or the same feature after assignment attempt) if its dynamic type is in fact one of a mutable string. So we think that the string passed as CONSTANT_GENERAL_STRING will not change, but it can. In fact it&#039;s not surprising. Even if the inheritance is appealing, a mutable string is not a constant string. It&#039;s like having RECTANGLE inherit from SQUARE. Hmmm, so I guess that in order to be 100% safe we need conversion in both ways. For comparison and performance, we probably need a common ancestor to STRING_GENERAL and CONSTANT_STRING_GENERAL. READONLY_STRING_GENERAL? We can only read the content of the string through this interface, but it&#039;s not necessarily a constant string. Polymorphically it can be attached to a mutable string whose content can be modified.&lt;/p&gt;

</description>
 <pubDate>Tue, 17 Apr 2007 00:57:39 -0700</pubDate>
 <dc:creator>ericb</dc:creator>
 <guid isPermaLink="false">comment 202 at http://www.eiffelroom.com</guid>
</item>
<item>
 <title>Questionnaire</title>
 <link>http://www.eiffelroom.com/blog/colin_adams/read_only_strings#comment-201</link>
 <description>&lt;p&gt;It might be worth running a poll on this. Options such as: &lt;ol&gt;
    &lt;li&gt; Keep the status Quo&lt;/li&gt;
    &lt;li&gt; Have STRING_GENERAL inherit CONSTANT_STRING_GENERAL (breaking all existing Eiffel code)&lt;/li&gt;
    &lt;li&gt; as my suggestion, despite the performance implications&lt;/li&gt;
&lt;/ol&gt;

&lt;/p&gt;

&lt;p&gt;Colin Adams&lt;/p&gt;

</description>
 <pubDate>Mon, 16 Apr 2007 10:14:08 -0700</pubDate>
 <dc:creator>colin-adams</dc:creator>
 <guid isPermaLink="false">comment 201 at http://www.eiffelroom.com</guid>
</item>
<item>
 <title>I agree</title>
 <link>http://www.eiffelroom.com/blog/colin_adams/read_only_strings#comment-200</link>
 <description>&lt;p&gt;Immutable STRING variants are something that I&#039;ve brought up a number of times. I fully agree that Eiffel needs immutable strings. Keep up the comments.&lt;/p&gt;

</description>
 <pubDate>Mon, 16 Apr 2007 09:27:30 -0700</pubDate>
 <dc:creator>paulbates</dc:creator>
 <guid isPermaLink="false">comment 200 at http://www.eiffelroom.com</guid>
</item>
<item>
 <title>Performance of string comparisons</title>
 <link>http://www.eiffelroom.com/blog/colin_adams/read_only_strings#comment-195</link>
 <description>&lt;p&gt;The disadvantage of using convert is that the cost of string comparisons, already an expensive operation, is increased by the need to create a temporary object. Colin Adams&lt;/p&gt;

</description>
 <pubDate>Sat, 14 Apr 2007 08:23:13 -0700</pubDate>
 <dc:creator>colin-adams</dc:creator>
 <guid isPermaLink="false">comment 195 at http://www.eiffelroom.com</guid>
</item>
<item>
 <title>CONSTANT_STRING</title>
 <link>http://www.eiffelroom.com/blog/colin_adams/read_only_strings#comment-194</link>
 <description>&lt;p&gt;&lt;code class=&quot;geshifilter eiffel&quot;&gt;CONSTANT_STRING_GENERAL&lt;/code&gt;, &lt;code class=&quot;geshifilter eiffel&quot;&gt;CONSTANT_STRING_8&lt;/code&gt; and &lt;code class=&quot;geshifilter eiffel&quot;&gt;CONSTANT_STRING_32&lt;/code&gt; look like better names. Colin Adams&lt;/p&gt;

</description>
 <pubDate>Fri, 13 Apr 2007 23:49:50 -0700</pubDate>
 <dc:creator>colin-adams</dc:creator>
 <guid isPermaLink="false">comment 194 at http://www.eiffelroom.com</guid>
</item>
<item>
 <title>Mutable strings are evil</title>
 <link>http://www.eiffelroom.com/blog/colin_adams/read_only_strings#comment-193</link>
 <description>&lt;p&gt;I, too, often encounter hard-to-track-down bugs in Eiffel code caused by the fact that STRING is mutable. Every other OO language that I&#039;ve worked with (Delphi, C#, Java) treats strings as immutable. This is a bit weird, because it means that strings are reference types with some expanded semantics; so the Eiffel approach is more consistent with the rest of the type system. But having a few years of Eiffel development under my belt now, I can safely say that it&#039;s not just a prejudice based on what I&#039;m used to: mutable strings are evil!&lt;/p&gt;

</description>
 <pubDate>Fri, 13 Apr 2007 20:22:35 -0700</pubDate>
 <dc:creator>peter_gummer</dc:creator>
 <guid isPermaLink="false">comment 193 at http://www.eiffelroom.com</guid>
</item>
<item>
 <title>Read-only strings</title>
 <link>http://www.eiffelroom.com/blog/colin_adams/read_only_strings</link>
 <description>&lt;p&gt;Why aren&#039;t literal strings in Eiffel read-only? Indeed, why isn&#039;t &lt;code class=&quot;geshifilter eiffel&quot;&gt;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+STRING&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;STRING&lt;/span&gt;&lt;/a&gt;&lt;/code&gt; read-only? I think (as do others) that Java gets in right in this respect, with it&#039;s separation of &lt;code class=&quot;geshifilter java5&quot;&gt;&lt;a href=&quot;http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html&quot;&gt;&lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;String&lt;/span&gt;&lt;/a&gt;&lt;/code&gt; and &lt;code class=&quot;geshifilter java5&quot;&gt;&lt;a href=&quot;http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html&quot;&gt;&lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;StringBuffer&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;All sorts of hard-to-debug problems can arise because there is no read-only variant of &lt;code class=&quot;geshifilter eiffel&quot;&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+STRING&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;STRING&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;. We had one of these occur in the Gobo XML library - the XML parser was corrupted because one of the event filters was editing one of the strings emitted by the parser.&lt;/p&gt;

&lt;p&gt;Our work-around for this problem was the following class:&lt;/p&gt;

&lt;p&gt;&lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;indexing&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; description: &lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;STRINGs with copy-on-write semantics&amp;quot;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #603000;&quot;&gt;library&lt;/span&gt;: &lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;Gobo Eiffel String Library&amp;quot;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; copyright: &lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;Copyright (c) 2005, Colin Adams and others&amp;quot;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; license: &lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;MIT License&amp;quot;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; date: &lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;$Date: 2007-01-26 18:55:25 +0000 (Fri, 26 Jan 2007) $&amp;quot;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; revision: &lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;$Revision: 5877 $&amp;quot;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;class&lt;/span&gt; interface&lt;br /&gt;
&amp;nbsp; &amp;nbsp; ST_COPY_ON_WRITE_STRING&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;create&lt;/span&gt; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; make&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;feature&lt;/span&gt; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- Access&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; item: STRING_8&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- String&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; safe_item: STRING_8&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- Version of item that is safe for editing&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;ensure&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; safe_to_edit: changed&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; same_as_item: &lt;span style=&quot;color: #800080;&quot;&gt;Result&lt;/span&gt; /= &lt;span style=&quot;color: #800080;&quot;&gt;Void&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;then&lt;/span&gt; &lt;span style=&quot;color: #800080;&quot;&gt;Result&lt;/span&gt; = item&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;feature&lt;/span&gt; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- Element change&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; append_character &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;c: CHARACTER_8&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- Append `c&#039; at end.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;ensure&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; new_count: item.&lt;span style=&quot;color: #000060;&quot;&gt;count&lt;/span&gt; = &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;old&lt;/span&gt; item.&lt;span style=&quot;color: #000060;&quot;&gt;count&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; appended: item.&lt;span style=&quot;color: #000060;&quot;&gt;item&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;item.&lt;span style=&quot;color: #000060;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt; = c&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; safe_to_edit: changed&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; append_string &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;s: STRING_8&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- Append a copy of `s&#039; at end.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;require&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; s_not_void: s /= &lt;span style=&quot;color: #800080;&quot;&gt;Void&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;ensure&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; safe_to_edit: changed&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; fill_with &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;c: CHARACTER_8&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- Replace every character with `c&#039;.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;ensure&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; same_count: &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;old&lt;/span&gt; item.&lt;span style=&quot;color: #000060;&quot;&gt;count&lt;/span&gt; = item.&lt;span style=&quot;color: #000060;&quot;&gt;count&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; filled: item.&lt;span style=&quot;color: #000060;&quot;&gt;occurrences&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;c&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt; = item.&lt;span style=&quot;color: #000060;&quot;&gt;count&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; safe_to_edit: changed&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; insert_character &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;c: CHARACTER_8; i: INTEGER_32&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- Insert `c&#039; at index `i&#039;, shifting characters between&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- ranks `i&#039; and `count&#039; rightwards.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;require&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; valid_insertion_index: &lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt; &amp;lt;= i &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;and&lt;/span&gt; i &amp;lt;= item.&lt;span style=&quot;color: #000060;&quot;&gt;count&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;ensure&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; one_more_character: item.&lt;span style=&quot;color: #000060;&quot;&gt;count&lt;/span&gt; = &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;old&lt;/span&gt; item.&lt;span style=&quot;color: #000060;&quot;&gt;count&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; inserted: item.&lt;span style=&quot;color: #000060;&quot;&gt;item&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;i&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt; = c&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; safe_to_edit: changed&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; put &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;c: CHARACTER_8; i: INTEGER_32&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- Replace character at index `i&#039; by `c&#039;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;require&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; valid_index: item.&lt;span style=&quot;color: #000060;&quot;&gt;valid_index&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;i&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;ensure&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; stable_count: item.&lt;span style=&quot;color: #000060;&quot;&gt;count&lt;/span&gt; = &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;old&lt;/span&gt; item.&lt;span style=&quot;color: #000060;&quot;&gt;count&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; replaced: item.&lt;span style=&quot;color: #000060;&quot;&gt;item&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;i&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt; = c&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; safe_to_edit: changed&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;invariant&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; item_not_void: item /= &lt;span style=&quot;color: #800080;&quot;&gt;Void&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- class ST_COPY_ON_WRITE_STRING&lt;/span&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Hm. Now I look at it, it seems that the contract for &lt;code class=&quot;geshifilter eiffel&quot;&gt;append_string&lt;/code&gt; could be strengthened in the postconditions.&lt;/p&gt;

&lt;p&gt;The _8 suffixes are only present because I used the EiffelStudio interface view.&lt;/p&gt;

&lt;p&gt;This class can be used in a lot of situations to avoid problems. The basic idea is to avoid duplicating strings needlessly.&lt;/p&gt;

&lt;p&gt;But it would be much better if we could follow the Java line here.&lt;/p&gt;

&lt;p&gt;I think it ought to be possible to do this without breaking (much) existing code, by making use of the convert keyword. Rather than having &lt;code class=&quot;geshifilter eiffel&quot;&gt;STRING_GENERAL&lt;/code&gt; inherit from &lt;code class=&quot;geshifilter eiffel&quot;&gt;READ_ONLY_STRING_GENERAL&lt;/code&gt;, we have parallel hierarchies, and say that the read-only versions can convert to the existing versions (the creation procedures involved would of course copy the characters of the string).&lt;/p&gt;

&lt;p&gt;Then we could change string literals to be of type &lt;code class=&quot;geshifilter eiffel&quot;&gt;READ_ONLY_STRING&lt;/code&gt; (or rather, one of its aliases), and everything should work just fine.&lt;/p&gt;

</description>
 <comments>http://www.eiffelroom.com/blog/colin_adams/read_only_strings#comments</comments>
 <category domain="http://www.eiffelroom.com/tag/string">string</category>
 <pubDate>Fri, 13 Apr 2007 11:52:36 -0700</pubDate>
 <dc:creator>colin-adams</dc:creator>
 <guid isPermaLink="false">164 at http://www.eiffelroom.com</guid>
</item>
</channel>
</rss>
