Probably stupid... question about string printString.

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

Probably stupid... question about string printString.

Stephane Ducasse-3
Hi

I would like to really understand why
'foo' printString
>>>
'''foo'''

and not why this is not

'foo'

itself.

Stef

Reply | Threaded
Open this post in threaded view
|

Re: Probably stupid... question about string printString.

alistairgrant
On Sat, Oct 21, 2017 at 05:28:21PM +0200, Stephane Ducasse wrote:

> Hi
>
> I would like to really understand why
> 'foo' printString
> >>>
> '''foo'''
>
> and not why this is not
>
> 'foo'
>
> itself.
>
> Stef

I can't comment on the original thinking behind this, but I agree that
something like:


String>>printOn: aStream
        "Represent the receiver on the supplied stream"

        aStream nextPutAll: self



would be much better.

If we're displaying values such as numbers and strings in a UI, e.g.
table, we normally don't want the strings always enclosed in quotes.

Cheers,
Alistair


Reply | Threaded
Open this post in threaded view
|

Re: Probably stupid... question about string printString.

Stephane Ducasse-3
Yes this is my point. I was surprised to get ' on the transcript.

Stef

On Sat, Oct 21, 2017 at 7:49 PM, Alistair Grant <[hidden email]> wrote:

> On Sat, Oct 21, 2017 at 05:28:21PM +0200, Stephane Ducasse wrote:
>> Hi
>>
>> I would like to really understand why
>> 'foo' printString
>> >>>
>> '''foo'''
>>
>> and not why this is not
>>
>> 'foo'
>>
>> itself.
>>
>> Stef
>
> I can't comment on the original thinking behind this, but I agree that
> something like:
>
>
> String>>printOn: aStream
>         "Represent the receiver on the supplied stream"
>
>         aStream nextPutAll: self
>
>
>
> would be much better.
>
> If we're displaying values such as numbers and strings in a UI, e.g.
> table, we normally don't want the strings always enclosed in quotes.
>
> Cheers,
> Alistair
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Probably stupid... question about string printString.

Nicolas Cellier
No. nextPutAll: self is not a good solution at all.
Try it, and you'll get foo, not 'foo'.

'foo' printString has exactly 5 letters: $' $f $o $o $' so it does what you want.

The problem is when you want to print 'foo' printString.
You are in fact doing 'foo' printString printOn: aStream, IOW aStream nextPutAll: 'foo' printString printString.

2017-10-21 21:12 GMT+02:00 Stephane Ducasse <[hidden email]>:
Yes this is my point. I was surprised to get ' on the transcript.

Stef

On Sat, Oct 21, 2017 at 7:49 PM, Alistair Grant <[hidden email]> wrote:
> On Sat, Oct 21, 2017 at 05:28:21PM +0200, Stephane Ducasse wrote:
>> Hi
>>
>> I would like to really understand why
>> 'foo' printString
>> >>>
>> '''foo'''
>>
>> and not why this is not
>>
>> 'foo'
>>
>> itself.
>>
>> Stef
>
> I can't comment on the original thinking behind this, but I agree that
> something like:
>
>
> String>>printOn: aStream
>         "Represent the receiver on the supplied stream"
>
>         aStream nextPutAll: self
>
>
>
> would be much better.
>
> If we're displaying values such as numbers and strings in a UI, e.g.
> table, we normally don't want the strings always enclosed in quotes.
>
> Cheers,
> Alistair
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Probably stupid... question about string printString.

Nicolas Cellier
IOW, if you want to print 'foo', just print 'foo', but don't print 'foo' printString.

2017-10-21 22:10 GMT+02:00 Nicolas Cellier <[hidden email]>:
No. nextPutAll: self is not a good solution at all.
Try it, and you'll get foo, not 'foo'.

'foo' printString has exactly 5 letters: $' $f $o $o $' so it does what you want.

The problem is when you want to print 'foo' printString.
You are in fact doing 'foo' printString printOn: aStream, IOW aStream nextPutAll: 'foo' printString printString.


2017-10-21 21:12 GMT+02:00 Stephane Ducasse <[hidden email]>:
Yes this is my point. I was surprised to get ' on the transcript.

Stef

On Sat, Oct 21, 2017 at 7:49 PM, Alistair Grant <[hidden email]> wrote:
> On Sat, Oct 21, 2017 at 05:28:21PM +0200, Stephane Ducasse wrote:
>> Hi
>>
>> I would like to really understand why
>> 'foo' printString
>> >>>
>> '''foo'''
>>
>> and not why this is not
>>
>> 'foo'
>>
>> itself.
>>
>> Stef
>
> I can't comment on the original thinking behind this, but I agree that
> something like:
>
>
> String>>printOn: aStream
>         "Represent the receiver on the supplied stream"
>
>         aStream nextPutAll: self
>
>
>
> would be much better.
>
> If we're displaying values such as numbers and strings in a UI, e.g.
> table, we normally don't want the strings always enclosed in quotes.
>
> Cheers,
> Alistair
>
>



Reply | Threaded
Open this post in threaded view
|

Re: Probably stupid... question about string printString.

alistairgrant
Hi Nicolas,

On Sat, Oct 21, 2017 at 10:12:06PM +0200, Nicolas Cellier wrote:

> IOW, if you want to print 'foo', just print 'foo', but don't print 'foo'
> printString.
>
> 2017-10-21 22:10 GMT+02:00 Nicolas Cellier <[hidden email]>
> :
>
>     No. nextPutAll: self is not a good solution at all.
>     Try it, and you'll get foo, not 'foo'.
>
>     'foo' printString has exactly 5 letters: $' $f $o $o $' so it does what you
>     want.

But what I want is the 3 letters in foo.

Think about displaying a table of values.  If it contains a mixture of
numbers and strings, what I want to see is:

1 | string one | 3.1415 | string two

not:

1 | 'string one' | 3.1415 | 'string two'


If I want the second form (with quotes) I can use #storeString / #storeOn:.

What is the one message that I can send to a mixture of numbers and
strings that will return a string (or write to a stream) that will
display as shown in the first example above?


Thanks,
Alistair



>     The problem is when you want to print 'foo' printString.
>     You are in fact doing 'foo' printString printOn: aStream, IOW aStream
>     nextPutAll: 'foo' printString printString.
>
>
>     2017-10-21 21:12 GMT+02:00 Stephane Ducasse <[hidden email]>:
>
>         Yes this is my point. I was surprised to get ' on the transcript.
>
>         Stef
>
>         On Sat, Oct 21, 2017 at 7:49 PM, Alistair Grant <[hidden email]>
>         wrote:
>         > On Sat, Oct 21, 2017 at 05:28:21PM +0200, Stephane Ducasse wrote:
>         >> Hi
>         >>
>         >> I would like to really understand why
>         >> 'foo' printString
>         >> >>>
>         >> '''foo'''
>         >>
>         >> and not why this is not
>         >>
>         >> 'foo'
>         >>
>         >> itself.
>         >>
>         >> Stef
>         >
>         > I can't comment on the original thinking behind this, but I agree
>         that
>         > something like:
>         >
>         >
>         > String>>printOn: aStream
>         >         "Represent the receiver on the supplied stream"
>         >
>         >         aStream nextPutAll: self
>         >
>         >
>         >
>         > would be much better.
>         >
>         > If we're displaying values such as numbers and strings in a UI, e.g.
>         > table, we normally don't want the strings always enclosed in quotes.
>         >
>         > Cheers,
>         > Alistair
>         >
>         >
>
>
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Probably stupid... question about string printString.

Torsten Bergmann
In reply to this post by alistairgrant
Alistair Grant wrote:
> I can't comment on the original thinking behind this

The original thinking is simple and as the name says: you want to print the
object (even when it is a string) to be print in a string.

So it always is a "printObjectInString" instead of a "printObjectAsString"
behavior. Also the resulting string often is a string to easily reconstruct the object.

So it is not a conversion method. There is #asString for that.


> If we're displaying values such as numbers and strings in a UI, e.g.
> table, we normally don't want the strings always enclosed in quotes.

There is #displayString for this task (which may return the printString or is overwritten)
and this is common in nearly all Smalltalks I know.

Bye
T.

Reply | Threaded
Open this post in threaded view
|

Re: Probably stupid... question about string printString.

Stephane Ducasse-3
In reply to this post by Nicolas Cellier
Nicolas

the problem is that I do not know if I will get a string or an object.
the arg gets printStringed (instead of asString).
Now if I want to have the correct behavior I will have to type check.

Still I do not understand why printString a string wrap it
'foo' printString
evaluating to 'foo'
vs.
'foo' printString
evaluating to '''foo'''

There is something unclear to me around string representation and
object representation as string.


@Alistair
In fact we should use displayString to call a printString for UI (list...)


On Sat, Oct 21, 2017 at 10:10 PM, Nicolas Cellier
<[hidden email]> wrote:

> No. nextPutAll: self is not a good solution at all.
> Try it, and you'll get foo, not 'foo'.
>
> 'foo' printString has exactly 5 letters: $' $f $o $o $' so it does what you
> want.
>
> The problem is when you want to print 'foo' printString.
> You are in fact doing 'foo' printString printOn: aStream, IOW aStream
> nextPutAll: 'foo' printString printString.
>
>
> 2017-10-21 21:12 GMT+02:00 Stephane Ducasse <[hidden email]>:
>>
>> Yes this is my point. I was surprised to get ' on the transcript.
>>
>> Stef
>>
>> On Sat, Oct 21, 2017 at 7:49 PM, Alistair Grant <[hidden email]>
>> wrote:
>> > On Sat, Oct 21, 2017 at 05:28:21PM +0200, Stephane Ducasse wrote:
>> >> Hi
>> >>
>> >> I would like to really understand why
>> >> 'foo' printString
>> >> >>>
>> >> '''foo'''
>> >>
>> >> and not why this is not
>> >>
>> >> 'foo'
>> >>
>> >> itself.
>> >>
>> >> Stef
>> >
>> > I can't comment on the original thinking behind this, but I agree that
>> > something like:
>> >
>> >
>> > String>>printOn: aStream
>> >         "Represent the receiver on the supplied stream"
>> >
>> >         aStream nextPutAll: self
>> >
>> >
>> >
>> > would be much better.
>> >
>> > If we're displaying values such as numbers and strings in a UI, e.g.
>> > table, we normally don't want the strings always enclosed in quotes.
>> >
>> > Cheers,
>> > Alistair
>> >
>> >
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: Probably stupid... question about string printString.

Stephane Ducasse-3
In reply to this post by alistairgrant
>
> But what I want is the 3 letters in foo.
>
> Think about displaying a table of values.  If it contains a mixture of
> numbers and strings, what I want to see is:
>
> 1 | string one | 3.1415 | string two
>
> not:
>
> 1 | 'string one' | 3.1415 | 'string two'
>
>
> If I want the second form (with quotes) I can use #storeString / #storeOn:.

indeed.
I do not get printString in String is storeOn:


> What is the one message that I can send to a mixture of numbers and
> strings that will return a string (or write to a stream) that will
> display as shown in the first example above?

Yes this is my point. In fact I do not know what I get. So now I'm
forced to type check.

Stef

>
>
> Thanks,
> Alistair
>
>
>
>>     The problem is when you want to print 'foo' printString.
>>     You are in fact doing 'foo' printString printOn: aStream, IOW aStream
>>     nextPutAll: 'foo' printString printString.
>>
>>
>>     2017-10-21 21:12 GMT+02:00 Stephane Ducasse <[hidden email]>:
>>
>>         Yes this is my point. I was surprised to get ' on the transcript.
>>
>>         Stef
>>
>>         On Sat, Oct 21, 2017 at 7:49 PM, Alistair Grant <[hidden email]>
>>         wrote:
>>         > On Sat, Oct 21, 2017 at 05:28:21PM +0200, Stephane Ducasse wrote:
>>         >> Hi
>>         >>
>>         >> I would like to really understand why
>>         >> 'foo' printString
>>         >> >>>
>>         >> '''foo'''
>>         >>
>>         >> and not why this is not
>>         >>
>>         >> 'foo'
>>         >>
>>         >> itself.
>>         >>
>>         >> Stef
>>         >
>>         > I can't comment on the original thinking behind this, but I agree
>>         that
>>         > something like:
>>         >
>>         >
>>         > String>>printOn: aStream
>>         >         "Represent the receiver on the supplied stream"
>>         >
>>         >         aStream nextPutAll: self
>>         >
>>         >
>>         >
>>         > would be much better.
>>         >
>>         > If we're displaying values such as numbers and strings in a UI, e.g.
>>         > table, we normally don't want the strings always enclosed in quotes.
>>         >
>>         > Cheers,
>>         > Alistair
>>         >
>>         >
>>
>>
>>
>>
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: Probably stupid... question about string printString.

alistairgrant
In reply to this post by Torsten Bergmann
Hi Torsten,

On Sun, Oct 22, 2017 at 10:40:41AM +0200, Torsten Bergmann wrote:

> Alistair Grant wrote:
> > I can't comment on the original thinking behind this
>
> The original thinking is simple and as the name says: you want to print the
> object (even when it is a string) to be print in a string.
>
> So it always is a "printObjectInString" instead of a "printObjectAsString"
> behavior. Also the resulting string often is a string to easily reconstruct the object.
>
> So it is not a conversion method. There is #asString for that.

Hmm, I knew about #asString, but this was a while ago, so I can't
remember why I didn't use it.  But thanks for the reminder.


> > If we're displaying values such as numbers and strings in a UI, e.g.
> > table, we normally don't want the strings always enclosed in quotes.
>
> There is #displayString for this task (which may return the printString or is overwritten)
> and this is common in nearly all Smalltalks I know.

Right, so what would be nice is for UI elements / GT toolkit to move to
using #displayString by default, and to add String>>displayString that
answers the receiver (currently in Pharo String uses the default
#printString, which returns the string with quotes).

From memory this has been discussed before, but I couldn't find it, and
don't remember the outcome.

Thanks,
Alistair