concatenation of non-Collections

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

concatenation of non-Collections

Mark Volkmann
Wouldn't it be convenient if you could do this:

        msg := 'The number is ', number

instead of

        msg := 'The number is ', number printString

Or even this:

        msg := number, ' is the value you entered.'

instead of

        msg := number printString, ' is the value you entered.'

This would require the comma method in Collection to act differently  
when the argument isn't a Collection
and the Number class would need to add a comma method.

I'm pretty new at this, so maybe there are reasons this is done. Are  
there?

As some might guess, I'm used to the way Java automatically calls  
toString on things that aren't strings when one side of a  
concatenation is a String and the other isn't.

---
Mark Volkmann




_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: concatenation of non-Collections

Ramon Leon-5
>
> Wouldn't it be convenient if you could do this:
>
> msg := 'The number is ', number

msg := 'The number is {1} or else is {2}' format: { number. number2 }

Behaves this way and looks much better when you have multiple args.

Ramon Leon
http://onsmaltalk.com

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: concatenation of non-Collections

keith1y
In reply to this post by Mark Volkmann
Mark Volkmann wrote:

> Wouldn't it be convenient if you could do this:
>
>     msg := 'The number is ', number
>
> instead of
>
>     msg := 'The number is ', number printString
>
> Or even this:
>
>     msg := number, ' is the value you entered.'
>
> instead of
>
>     msg := number printString, ' is the value you entered.'
>
> This would require the comma method in Collection to act differently
> when the argument isn't a Collection
> and the Number class would need to add a comma method.
>
> I'm pretty new at this, so maybe there are reasons this is done. Are
> there?
In general concatenation isn't the most efficient means to assemble a
string. I always build my strings using a stream, and to help
readability I have defined my extension #<< to stream.

out := String new writeStream.

out << 'The number is' << number

^ out contents

my definition of #<< calls putOn: on each item, and putOn: does ensure
that numbers are converted to strings first.

==
To avail yourself of this extension, execute (if you have installer loaded)

Installer mantis ensureFix: 7219.

best regards

Keith.
 
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: concatenation of non-Collections

Bert Freudenberg
In reply to this post by Mark Volkmann
Am 02.11.2008 um 12:34 schrieb Mark Volkmann <[hidden email]>:

> Wouldn't it be convenient if you could do this:
>
>    msg := 'The number is ', number
>
> instead of
>
>    msg := 'The number is ', number printString
>
> Or even this:
>
>    msg := number, ' is the value you entered.'
>
> instead of
>
>    msg := number printString, ' is the value you entered.'
>
> This would require the comma method in Collection to act differently  
> when the argument isn't a Collection
> and the Number class would need to add a comma method.

It's convenient indeed but could lead to surprises for non-string  
collections. Also, there is a school of thinking that abhors any  
"magic". It does change the semantics of #, depending on the passed  
object, so there is some reason nor to do it.

I personally find it useful, and IIRC Croquet implements String>>, as  
"^super , anObject asString."

- Bert -
 
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: concatenation of non-Collections

Bert Freudenberg
In reply to this post by Ramon Leon-5

On 02.11.2008, at 12:49, Ramon Leon wrote:

>>
>> Wouldn't it be convenient if you could do this:
>>
>> msg := 'The number is ', number
>
> msg := 'The number is {1} or else is {2}' format: { number. number2 }
>
> Behaves this way and looks much better when you have multiple args.


Very good point - this format is also better for translating.

It's probably good practice to use  "," only for debug output - and  
there it is useful because it saves keystrokes.

- Bert -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners