From: robert@metropolis.com (Robert Munyer)
Subject: Re: Yet Another Beginner's Question...
Organization: Metropolis Software, Inc.
Date: Thu, 13 Aug 1992 07:59:36 GMT

In article <1992Aug12.133736.1@uwovax.uwo.ca> brent@uwovax.uwo.ca (Brent
Sterner) writes:

>    Me again with another beginner's question.

I'm a beginner too, but at least I'm pretty sure I can answer this one :-) !
In fact, sometimes it is BETTER to get your answer from another beginner.

>    I have the following code fragment in the displayOn method for a view:

> 		(self graphicsContext)
> 			paint: ColorValue black;
> 			displayRectangle: graphBar.

What you are doing here is sending the message "black" to the class
ColorValue, and then using the value it returns as an argument to "paint:".

>    I would like to vary the colour from black to other colours, depending
>    on various conditions.  I cannot get the simplest case to work:

> 		| colour |
> 		colour := black.

>         	(self graphicsContext)
>                 	paint: ColorValue colour;
> 	                displayRectangle: graphBar.

What you are doing here is sending the message "colour" to the class
ColorValue, and ColorValue does not understand it.  It doesn't matter that
"colour" also happens to be a variable; you are using it as a message.

Try this:

    | colour |
    colour := ColorValue black.

    (self graphicsContext)
        paint: colour;
        displayRectangle: graphBar.

If you did this, you would be sending the message "black" to the class
ColorValue (just like in your first example), and then assigning the value it
returns as the value of the variable "colour".  You then use the variable
"colour" to supply the value that is passed to the message "paint:".  The key
concept you need to understand is that when you use a symbol as a MESSAGE
(e.g. "ColorValue colour"), the symbol is not evaluated -- that is, its value
does not matter.  But when you use the symbol as a VALUE (e.g. "paint:
colour"), it IS evaluated.

Here is another way of doing it.  This is a more direct way of transforming
your second example into one that works.  But it is more complicated than,
and therefore not as good as, the simple solution above.

    | colour |
    colour := #black.

    (self graphicsContext)
        paint: (ColorValue perform: colour);
        displayRectangle: graphBar.

If you did this, you would be assigning the symbol "black" as the value of the
variable "colour", and then using that variable to supply the message that is
passed to the class "ColorValue" in order to get the value that is used as the
argument to the message "paint:".  Like I said, too complicated.

[Note to the experts out there:  I am designing an OO language that does not
have this feature -- variables can be used as messages, in which case the value
of the variable determines which message will be sent.  Do you think I am
making a mistake?  Is there something I don't know about that will bite me?]

______________________________________________________________________
Robert Munyer            |   "I may be synthetic, but I'm not stupid."
robert@metropolis.com    |      -- Bishop, _Aliens_

P.S.  Please don't flame me if I made a typo.  Ordinarily I don't believe in
posting untested code to the Net, but my system doesn't support color so I
can't test this code.

Newsgroups: comp.lang.smalltalk