Newbie Needs syntax clarification

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

Newbie Needs syntax clarification

acg
HI everyone
What does '##' mean ?

A real-life example, as in
View>>defaultExtent which returns ##(100 @100).

Thanks
ACG


Reply | Threaded
Open this post in threaded view
|

Re: Newbie Needs syntax clarification

Bill Schwab-2
Andrew,

> What does '##' mean ?
>
> A real-life example, as in
> View>>defaultExtent which returns ##(100 @100).

It's a compile-time evaluation for efficiency.  It becomes even more
important for collections that are always the same - see
Canvas>>textExtent:width:alignment:.

Have a good one,

Bill

--
Wilhelm K. Schwab, Ph.D.
[hidden email]


acg
Reply | Threaded
Open this post in threaded view
|

Re: Newbie Needs syntax clarification

acg
Hi Bill-
>> What does '##' mean ?
>

Thank for your reply. What do you say in English, i.e. "symbol of array of
point" or "symbol of symbol of point", or what ?

Thanks again,
ACG


Reply | Threaded
Open this post in threaded view
|

Re: Newbie Needs syntax clarification

Ian Bartholomew-4
Andrew,

> Thank for your reply. What do you say in English, i.e. "symbol of array of
> point" or "symbol of symbol of point", or what ?

The ## construction is normally called something like a "compile time
evaluation". Perhaps a bit of clarification -

##(aSmalltalkExpression) is a way of adding an object to a method at compile
time. The effect is that when the method is saved (compiled)
aSmalltalkExpression is evaluated and the resulting object embedded in the
method, effectively as a literal. For example, adding the following to any
class will result in a halt when the method is accepted (press resume from
the resulting walkback) ...

Aaa>>test
    ^##(self halt)

... but it will not halt when the method is executed, by evaluating "AAA new
test". Why? Because when the method was compiled "self halt" was evaluated
(and caused a walkback) and the result of this expression is the class in
which the halt occurred. After compilation the above method is therefore
equivalent to

Aaa>>test
    ^Aaa

## is normally used for defining constant values at compile time to avoid
unnecessary delays when an application is run.  If a method was defined as

Aaa>>constants
    ^LookupTable new
        at: 'const1' put: 2.345 * Float pi * 9.876e-10;
        at: 'const2' put: 2.345 * (Float pi * 2) * (9.876e-10 / 2);
        &etc

then every time that constants was called all the mathematical expressions
would have to be re-evaluated. By enclosing it in a ## then they are only
evaluated once, at compile time.

Aaa>>constants
    ^##(LookupTable new
        at: 'const1' put: 2.345 * Float pi * 9.876e-10;
        at: 'const2' put: 2.345 * (Float pi * 2) * (9.876e-10 / 2);
        &etc)

One thing to be careful of is that the value returned is a singleton, only
one instance of it exists and the same object is answered every time the
method is evaluated. If you define

Aaa>>testPoint
    ^##(100@200)

then

a := Aaa new testPoint.
b := Aaa new testPoint.
a == b --> true

but then

b x = 100. --> true.
a x: 123.
b x = 100 --> false (b now also contains 123@200)

So if you use ## make sure that you only modify objects created in this way
if you want to modify _every_ occurrence of the returned value.

Regards
    Ian


acg
Reply | Threaded
Open this post in threaded view
|

Re: Newbie Needs syntax clarification

acg
Hi Ian>
>The ## construction is normally called something like a "compile time

Thank.. this clarifies it. I have not looked into the compiler time stuff. What
you say reminds me of weird stuff one could do in FORTH !!,
Thanks,
Andy