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
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
Hi again,
having re-read your message, here's the next blob. :-)
On Sun, Nov 15, 2009 at 8:02 PM, Andy Burnett andy.burnett@knowinnovation.com 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
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:Lou@Keystone-Software.com http://www.Keystone-Software.com
beginners@lists.squeakfoundation.org