Defining a binary message selector

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

Defining a binary message selector

Andy Burnett
As an experiment, I tried to create a Fibonacci method for Integer.  Initially, I defined it as Integer>>fibonacci: aNumber. However, having thought about it a bit more I realised that it should probably be a binary message like '+'. I tried to create it as such, but Squeak wouldn't let me - even when I copied the code from the '+' method.

After a bit of head scratching, I decided that '+' was probably a symbol, and that binary messages are probably limited to using symbols as selectors. However, I once arrived late to a baseball game (never having seen it played before), got confused about which team was which, and invented an entirely new scoring system that pretty much explained the results on the scoreboard.  So, I may be completely wrong about binary selectors!

If I am right about them requiring to be symbols.  Would it be a good idea for meto make 'fibonacci' a symbol as well? Or would that lead to unintended problems down the road?

Cheers
Andy

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

Re: Defining a binary message selector

Michael Haupt-3
Hi Andy,

why would the message have to be binary anyway? If you want the k-th
Fibonacci number, why don't you just send #fib to k?

Regarding symbols vs. not symbols: all selectors are symbols,
internally. I might get something in your e-mail wrong - could you
restate the question?

Best,

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

Re: Defining a binary message selector

Michael Haupt-3
In reply to this post by Andy Burnett
Hi again,

having re-read your message, here's the next blob. :-)

On Sun, Nov 15, 2009 at 8:02 PM, Andy Burnett
<[hidden email]> wrote:
> After a bit of head scratching, I decided that '+' was probably a symbol,
> and that binary messages are probably limited to using symbols as selectors.

Yup, but they're rather called "special characters". Symbols are those
unique Strings that begin with a # character - and, as I wrote
earlier, all selectors are, internally, represented as Symbols.

What was the precise message that Squeak gave you when you tried to
implement +? It might be that replacing an existing implementation of
+ with another is prohibited, depending on the class you want to do it
in. (Number might well be an example of that.)

Best,

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

Re: Defining a binary message selector

Andy Burnett
In reply to this post by Andy Burnett
Michael said <<

Hi Andy,

why would the message have to be binary anyway? If you want the k-th
Fibonacci number, why don't you just send #fib to k?

Regarding symbols vs. not symbols: all selectors are symbols,
internally. I might get something in your e-mail wrong - could you
restate the question?

>>

Hi Michael
Thanks very much for your thoughts. Let me try to explain myself, slightly more clearly!

First, this was just an experiment. I was reading a book on Python, saw how they did it, and thought I would try in Squeak. So, I hadn't really thought about how I was going to use it.  I like your suggestion about implementing is as:  anInteger #fib.  However, thinking about implementing it as a binary message made me curious about why I couldn't do that.

The specific problem I am having is:
If I define an Integer method such as

<<<*** aNumber

Squeak is quite happy to let me create it.  However, if I do something like    fib aNumber, the compiler complains that aNumber is a unknown variable, which I need to define.  So, what I was really trying to understand was what it was about the e.g. <<< symbol which allowed it to have an undeclared argument.

I think that, based on your last message, it is just that <<< etc have been defined as special characters. Is that correct?

Cheers
Andy

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

Re: Re: Defining a binary message selector

Michael Haupt-3
Hi Andy,

On Sun, Nov 15, 2009 at 11:19 PM, Andy Burnett
<[hidden email]> wrote:
> The specific problem I am having is:
> If I define an Integer method such as
>
> <<<*** aNumber
>
> Squeak is quite happy to let me create it.  However, if I do something
> like    fib aNumber, the compiler complains that aNumber is a unknown
> variable, which I need to define.

this is simpler than you may have thought. :-)

It's all about syntaaaaaaaaaaaaaaaaaaax. If you want to implement the
method as a binary message (with special characters), everything is
fine - as you yourself noticed. But if you want to implement it as a
so-called keyword message (i.e., where the selector consists of
alphanumeric characters), you have to insert a colon (:) after each of
the keywords.

The solution would be not to write

fib aNumber

but

fib: aNumber

instead. That way, the parser knows where to look for parameters. ;-)

(((And if you have keyword messages with multiple parameters, use a
colon wherever a parameter needs to be placed, e.g., fib: aNumber fob:
anotherNumber fub: whatever - just browse the image to see how the
different things are done. But I assume you actually know that.)))

> So, what I was really trying to
> understand was what it was about the e.g. <<< symbol which allowed it to
> have an undeclared argument.

The argument to a binary message is not undeclared; the simple fact
that the message is binary *implies* there will be a parameter.

Am I making sense?

Best,

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

Re: Defining a binary message selector

Andy Burnett
In reply to this post by Andy Burnett
Michael said <<

If you want to implement the method as a binary message (with special characters), everything is fine

>>

Brilliant, so I was on the right track - sort of!  Now for the important question.  Where are the special characters defined?  I looked in Smalltalk, and found a SpecialObjectsArray  (along with a warning saying "don't touch this"!).  I am not planning to touch it, but I am curious to understand how the system works.

<<
The argument to a binary message is not undeclared; the simple fact
that the message is binary *implies* there will be a parameter.

Am I making sense?

>>

Totally, that clears it up for me.

Cheers
Andy




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

Re: Re: Defining a binary message selector

Michael Haupt-3
Hi Andy,

On Mon, Nov 16, 2009 at 4:49 PM, Andy Burnett
<[hidden email]> wrote:
> Brilliant, so I was on the right track - sort of!  Now for the important
> question.  Where are the special characters defined?  I looked in Smalltalk,
> and found a SpecialObjectsArray  (along with a warning saying "don't touch
> this"!).  I am not planning to touch it, but I am curious to understand how
> the system works.

eh, I guess you're looking for that bit of the Squeak scanner that
initialises the character tables ... look at Scanner class >>
#initialize.

Best,

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

Re: Defining a binary message selector

Louis LaBrunda
In reply to this post by Andy Burnett
Hi Andy,

I may be way off in my understanding of what you are trying to do, if so I
apologize ahead of time.

It looks to me like you want to create a method that will answer the
Fibonacci value for a given number.  Hence your first attempt
Integer>>fibonacci: aNumber.  Where I expect you returned the Fibonacci
value of aNumber.  I think you then moved on to wanting to just a message
and not having to pass aNumber.

This lead to the next attempt with '+'.  This is where I think you went a
little wrong, '+' actually has a parameter.  It normally looks like
someNumber + aNumber or 2 + 3.  Others have suggested using something like
#fib, but didn't explain much more.  This is the way to go, unless you
really want to use some single character, which you could.

If you extend Integer with the #fib method (without the # and no parameter)
you should have what you want.  All you need to do is use the code you had
for Integer>>fibonacci: aNumber and everywhere you had aNumber, replace it
with "self", you will be able to send the #fib message to any integer (like
5 fib) and get the Fibonacci value of the integer the #fib message was sent
to.

I will leave it to others to tell you how to use some special character to
replace #fib, if that is really what you want to do.

Have fun!

Lou

>As an experiment, I tried to create a Fibonacci method for Integer.
>Initially, I defined it as Integer>>fibonacci: aNumber. However, having
>thought about it a bit more I realised that it should probably be a binary
>message like '+'. I tried to create it as such, but Squeak wouldn't let me -
>even when I copied the code from the '+' method.
>
>After a bit of head scratching, I decided that '+' was probably a symbol,
>and that binary messages are probably limited to using symbols as selectors.
>However, I once arrived late to a baseball game (never having seen it played
>before), got confused about which team was which, and invented an entirely
>new scoring system that pretty much explained the results on the
>scoreboard.  So, I may be completely wrong about binary selectors!
>
>If I am right about them requiring to be symbols.  Would it be a good idea
>for meto make 'fibonacci' a symbol as well? Or would that lead to unintended
>problems down the road?
>
>Cheers
>Andy
-----------------------------------------------------------
Louis LaBrunda
Keystone Software Corp.
SkypeMe callto://PhotonDemon
mailto:[hidden email] http://www.Keystone-Software.com

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