[Newbies] assorted beginner questions
Ron Teitelbaum
Ron at USMedRec.com
Wed Oct 3 14:07:45 UTC 2007
Hi Mark,
Welcome to the list, and Smalltalk.
> From: Mark Smithfield
>
> This is a mixed bag. These are the questions that I
> would ask an
> informed friend. They might be obvious, but they are
> not obvious
> to me (yet, I hope).
> -----------------------------
> As I read code in Squeak, I discover method names that
> I do not
> know. How do you find these methods if you don't know
> the type of
> the receiver? For example,
>
> self subclassResponsibility.
This is an error that a programmer uses to call attention to something that
another developer should have done. In Smalltalk we have something called
inheritance. This basically means that if you subclass an object the
subclass can be considered to have, or implement, all the methods of its
parent object. There are times when you want the subclass to provide its
own content in a method but you want to make sure that all subclasses
provide a valid answer to the message. In that case you implement the
method and have it call self subclassResponsibility. Which would raise and
error that says, your class should have overridden this method, or something
like that. This tells you that you made a mistake and should implement a
method with the same name.
For example:
A superclass named: StoreProduct
Might implement a
StoreProduct class >> isTaxible
"return a Boolean describing if product requires collecting
State Sales tax"
^self subclassResponsiblity
Then a subclass named: StoreProductFood
Would override this method with
StoreProductFood class >> isTaxible
"return a Boolean describing if product requires collecting
State Sales tax"
^false
Another subclass named: StoreProductNonFood
Would override this method with
StoreProductFood class >> isTaxible
"return a Boolean describing if product requires collecting
State Sales tax"
^self manufacturer hasStoreLocationIn: Store
currentLocationState.
Or something like that.
> -----------------------------
> In the floor method from Number,
>
> truncation _ self truncated.
>
> What does the underscore mean?
It is an assignment operator, it is much better to use :=
> -----------------------------
> I want to apply smalltalk to fibonacci numbers. I add
> this method
> to Integer,
>
> fib:
> (self = 0) ifTrue: (^0)
> (self = 1) ifTrue: (^1)
> ^ (self - 1 fib) + (self - 2 fib).
>
> Next, I would like to memoize this method, (because of
> the
> enormous performance gains). I do not see how to
> memo-ize things
> in smalltalk. Can somebody help me see the necessary
> shift in
> thinking?
You need to create the method.
The method can not have a : after it unless you are providing a argument to
your method
So you would leave that out, then find the template by selecting the
protocol in your integer browser (mathematical functions), add the following
and accept it. This will save the method in the class Integer.
Integer >> fib
"return the Fibonacci sum for the receiver"
self > 0 ifFalse: [^0].
self = 1 ifTrue: [^1].
^(self - 1) fib + (self - 2) fib
Then you can collect the sequence by doing something like:
(1 to: 10) collect: [:i | i fib].
Highlight this and print it and get:
#(0 1 1 2 3 5 8 13 21 34 55)
>
> and
>
> Would a smalltalker even take this approach?
Sure, it makes sense.
> -----------------------------
>
> I am still waiting for things to >click< w/rt
> smalltalk. I know
> that I don't know and that's all that I know :)
>
> Thanks to all the people on this list for letting this
> list be a
> place where you can ask questions in this fashion w/o
> the
> certainty of abuse and dismissal.
>
> Mark.
>
>
You are welcome.
Ron Teitelbaum
President / Principal Software Engineer
US Medical Record Specialists
www.USMedRec.com
More information about the Beginners
mailing list