[squeak-dev] Re: Newbie Question: How does this work?
frank.shearar at gmail.com
Tue Oct 9 15:16:59 UTC 2012
On 9 October 2012 16:08, Joseph J Alotta <joseph.alotta at gmail.com> wrote:
> Thanks for you help.
> I see that String has a #printf and the protocol is *printf, and when the printf: method executes it looks everywhere for the FormatString class which is added to the end by the Printf package.
> So the question is how did the *printf protocols get added to String? It is not some magic by Monticello, because I did not use
> Monticello, I just filein-ed the code.
> So where in the code does it install the *printf methods to String?
The original code (in what's called "chunk format") was incomplete. If
it had been complete (like the Printf Chris Cunnington's mentioned)
you'd have seen something like this:
!String methodsFor: '*printf'!
^ self asFormatString printf: arguments! !
In Monticello these chunks are instead represented by MCDefinition subclasses.
> On Oct 9, 2012, at 7:00 AM, squeak-dev-request at lists.squeakfoundation.org wrote:
>> On 12-10-08 6:13 PM, Joseph J Alotta wrote:
>>> '%6.2e' printf: 412.343434
>> I loaded in the code you added in Squeak 4.3, executed the above, and
>> got "MessageNotUnderstood: ByteString>>printf:". I then loaded in Printf
>> from squeaksource.com/Printf and it worked.
>> Monticello added some extra methods to various classes in the image.
>> Don't look at FormatString>>printf: Look at String>>printf:, which is a
>> superclass of ByteString. The protocol list has printf with an * before
>> it - *printf. The Monticello package added some extra methods to the
>> String class.
>> Thank you for asking interesting questions and staying involved.
>> One way to find this out for yourself is to run this code to step through
>> what is happening (highlighting the two lines and hitting the PrintIt key
>> self halt.
>> '123%s456' printf: ('s').
>> You'll find that the class ByteString also now has a #printf:method
>> inherited from String class. So, the ByteString class knows how to
>> convert itself to a FormatString if it sees a #printf: method.
>> printf: arguments
>> ^ self asFormatString printf: arguments
>> The ' * ' in *printf method category name is a visual clue that a load has
>> added it to the default String class.
More information about the Squeak-dev