String printOn: / storeOn:

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

String printOn: / storeOn:

Camillo Bruni-3
I think that printing on streams could be improved by changing String >> #printOn: to

printOn: aStream
        aStream nextPutAll: self

because string is special when printing:

        String streamContents: [ :s| s print: 'asdf']
does NOT equal:
        String streamContents: [ :s| s nextPutAll: 'asdf' asString ]

whereas
        String streamContents: [ :s| s print: 1]
equals:
        String streamContents: [ :s| s nextPutAll: 1 asString ]


so for any other object
        s nextPutAll: object asString
equals
        s print: object

Now the same implication holds for Symbol and most probably this idea is in strong contrast to the blue book :D
cbc
Reply | Threaded
Open this post in threaded view
|

Re: String printOn: / storeOn:

cbc
On Tue, Jun 5, 2012 at 12:53 PM, Camillo Bruni <[hidden email]> wrote:
> I think that printing on streams could be improved by changing String >> #printOn: to
>
> printOn: aStream
>        aStream nextPutAll: self
>

Ah, but if you change it to this, then

       String streamContents: [ :s| s print: 'an Object']
equals:
       String streamContents: [ :s| s print: Object new ]

That is, from the printed version, you can't tell which one is
actually a string.  Which could get weird in Inpsectors.

-Chris

Reply | Threaded
Open this post in threaded view
|

Re: String printOn: / storeOn:

Camillo Bruni-3

On 2012-06-05, at 22:33, Chris Cunningham wrote:

> On Tue, Jun 5, 2012 at 12:53 PM, Camillo Bruni <[hidden email]> wrote:
>> I think that printing on streams could be improved by changing String >> #printOn: to
>>
>> printOn: aStream
>>        aStream nextPutAll: self
>>
>
> Ah, but if you change it to this, then
>
>       String streamContents: [ :s| s print: 'an Object']
> equals:
>       String streamContents: [ :s| s print: Object new ]
>
> That is, from the printed version, you can't tell which one is
> actually a string.  Which could get weird in Inspectors.

true :) but the inspector mostly works on the real object so I wouldn't care too much about that :)
Reply | Threaded
Open this post in threaded view
|

Re: String printOn: / storeOn:

Nicolas Cellier
The assumption was that a Smalltalk literal would print itself as a
Smalltalk literal.
I can't tell if this is a vital feature, and we also have storeOn:...
But I can tell you're gonna break a lot of expectations.

What are you looking for?
Something like C++stream << aString ?
Keith Hodge added such support

Stream>> << items
  items putOn: self.
        ^ self

Stream>>putOn:aStream
        ^aStream nextPutAll: self.

Or is it something else?

Nicolas

2012/6/5 Camillo Bruni <[hidden email]>:

>
> On 2012-06-05, at 22:33, Chris Cunningham wrote:
>
>> On Tue, Jun 5, 2012 at 12:53 PM, Camillo Bruni <[hidden email]> wrote:
>>> I think that printing on streams could be improved by changing String >> #printOn: to
>>>
>>> printOn: aStream
>>>        aStream nextPutAll: self
>>>
>>
>> Ah, but if you change it to this, then
>>
>>       String streamContents: [ :s| s print: 'an Object']
>> equals:
>>       String streamContents: [ :s| s print: Object new ]
>>
>> That is, from the printed version, you can't tell which one is
>> actually a string.  Which could get weird in Inspectors.
>
> true :) but the inspector mostly works on the real object so I wouldn't care too much about that :)

Reply | Threaded
Open this post in threaded view
|

Re: String printOn: / storeOn:

Camillo Bruni-3

On 2012-06-06, at 08:27, Nicolas Cellier wrote:

> The assumption was that a Smalltalk literal would print itself as a
> Smalltalk literal.

yeah I know, that IS a somehow a nice property :)

> I can't tell if this is a vital feature, and we also have storeOn:...

exactly. funnily I found a few bugs in storeOn:/printOn: methods by changing the string behavior

> But I can tell you're gonna break a lot of expectations.
actually that will be mostly on user-side. in my test-image I had to fix a couple of tests but it perfectly keeps on working :)

> What are you looking for?
> Something like C++stream << aString ?

well I find it a bit strange that String and Symbol behave different than all the other classes when it comes to printing. E.g. whenever you change an inst-var from String/Symbol to another class you'll have to adapt the #printOn: methods :)

> Keith Hodge added such support
>
> Stream>> << items
> items putOn: self.
> ^ self
>
> Stream>>putOn:aStream
> ^aStream nextPutAll: self.
>
> Or is it something else?
>
> Nicolas