Hi all,
I've got a strange problem here. The folowing code works in Squeak and Smalltalk Express , but not in VW: string := 'now is the time'. string at: 1 put: $N. string. (btw, this is from the book "Smalltalk by Example") I get a "no modification error". I looked at the code for at:put: and I found out , that 'now is the time ' is actually something like a literal for the system , because it is immutable. I tried " 'asdf' isImmutable " in WorkSpace and it returns true. So, my question is, how do I do this in VW? I thought of converting the string to ByteArray or some Collection, modify it , and then convert it back to ByteString but this is too complicated. There must be an easier way to do it. I know I'm missing something. |
Change the first line to
string := 'now is the time' copy. Unlike the original literal, the copy is mutable. On Nov 25, 2007 6:03 PM, Ivo Ardaliev <[hidden email]> wrote: > Hi all, > I've got a strange problem here. > The folowing code works in Squeak and Smalltalk Express , but not in VW: > > string := 'now is the time'. > string at: 1 put: $N. > string. [...] |
In reply to this post by Ivo Ardaliev
Hi Ivo:
Try: | string | string := 'now is the time' copy. string at: 1 put: $N. string. On Nov 25, 2007, at 6:03 PM, Ivo Ardaliev wrote: > Hi all, > I've got a strange problem here. > The folowing code works in Squeak and Smalltalk Express , but not in > VW: > > string := 'now is the time'. > string at: 1 put: $N. > string. > > (btw, this is from the book "Smalltalk by Example") > I get a "no modification error". > I looked at the code for at:put: and I found out , that 'now is the > time ' is actually something like a literal for the system , because > it is immutable. I tried " 'asdf' isImmutable " in WorkSpace and it > returns true. > So, my question is, how do I do this in VW? > I thought of converting the string to ByteArray or some > Collection, modify it , and then convert it back to ByteString but > this is too complicated. There must be an easier way to do it. I > know I'm missing something. > > Thanks!! Joseph Bacanskas [|] --- I use Smalltalk. My amp goes to eleven. |
In reply to this post by Ivo Ardaliev
Ivo Ardaliev wrote:
> Hi all, > I've got a strange problem here. > The folowing code works in Squeak and Smalltalk Express , but not in VW: > > string := 'now is the time'. > string at: 1 put: $N. > string. > > (btw, this is from the book "Smalltalk by Example") > I get a "no modification error". Sure. Your example above attempts to modify the compiled method itself (i.e. the literal string associated with it). As others have pointed out, making a copy of the compiled "resource" is the right thing to do. Without immutability, snippets like your example would accidentally modify code behind the scenes. Self-destructing code is a bad thing in itself, but in addition to that, the source code would not reflect the changes, further adding to the confusion :-) Andre |
In reply to this post by Ivo Ardaliev
If you have a real need, you can explicitly make something immutable
like so: 'foo' , 'bar' isImmutable: true But it is not recommended to arbitrarily make things mutable, especially literals, because the compiled method that holds it will no longer match its source. That's why no one has yet mentioned #isImmutable: to you, but instead suggested that you use #copy. So don't use #isImmutable: unless you really understand the consequences and really need it. But if you're learning about VW's immutability scheme you should at least know about it, if for no other reason than to know what not to do. Dave Ivo Ardaliev wrote: > Hi all, > I've got a strange problem here. > The folowing code works in Squeak and Smalltalk Express , but not in VW: > > string := 'now is the time'. > string at: 1 put: $N. > string. > > (btw, this is from the book "Smalltalk by Example") > I get a "no modification error". > I looked at the code for at:put: and I found out , that 'now is the > time ' is actually something like a literal for the system , because it > is immutable. I tried " 'asdf' isImmutable " in WorkSpace and it returns > true. > So, my question is, how do I do this in VW? > I thought of converting the string to ByteArray or some Collection, > modify it , and then convert it back to ByteString but this is too > complicated. There must be an easier way to do it. I know I'm missing > something. > > |
In reply to this post by Andre Schnoor
Andre Schnoor escreveu:
> Ivo Ardaliev wrote: >> Hi all, >> I've got a strange problem here. >> The folowing code works in Squeak and Smalltalk Express , but not in VW: >> >> string := 'now is the time'. >> string at: 1 put: $N. >> string. >> >> (btw, this is from the book "Smalltalk by Example") >> I get a "no modification error". > > Sure. Your example above attempts to modify the compiled method itself > (i.e. the literal string associated with it). As others have pointed > out, making a copy of the compiled "resource" is the right thing to do. > Not so sure! It attempts to modify a string which in other Smalltalk dialects is acceptable... > Without immutability, snippets like your example would accidentally > modify code behind the scenes. Self-destructing code is a bad thing in > itself, but in addition to that, the source code would not reflect the > changes, further adding to the confusion :-) This is a safety net decided and implemented in VW Smalltalk. Make sense we consider it 'natural' (this is VW list), but is not the only way it is implemented. My 0.019999... -- Cesar Rabak GNU/Linux User 52247. Get counted: http://counter.li.org/ |
Free forum by Nabble | Edit this page |