[Newbies] concatenation of non-Collections

Keith Hodges keith_hodges at yahoo.co.uk
Sun Nov 2 23:00:35 UTC 2008


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.
 


More information about the Beginners mailing list