Pushing #value up to Object

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

Pushing #value up to Object

G Krupa-2
All,

Can anyone think of unexpected consequences of pushing #value up to Object?
 From my (naive?) point of view this makes everything <niladic> and fun.  I
can now pass blocks or values into methods, and let lazy-init work its magic
if things happen just so.  I'm putting #value sends all over the place
now--wee!

I particularly like this for dictionaries used to store
preferences/settings.  The values can be static or computed when requested.
This was the itch that led me here.  I guess since Smalltalk doesn't really
perform computation on assignment like C++ does (copy constructors,
operator=(), etc.), this is the "next best thing" for those times when I
want something similar.

My concern is that I'm somehow going to shoot myself in the foot doing this.
Does anyone have any experience or wisdom to share?

Please excuse me if this was best posted to c.l.s instead, it's just that
it's so much quieter and sane in here...

Cheers,

--GK


Reply | Threaded
Open this post in threaded view
|

Re: Pushing #value up to Object

Chris Uppal-3
G Krupa wrote:

> Can anyone think of unexpected consequences of pushing #value up to
> Object? From my (naive?) point of view this makes everything <niladic>
> and fun.  I can now pass blocks or values into methods, and let lazy-init
> work its magic if things happen just so.  I'm putting #value sends all
> over the place now--wee!

I've wondered about the same thing.

I remember reading a post in c.l.s some years back that said something like
"the mess we got into with Object>>value", and I've several times contemplated
asking that community what the "bad" experience had been.

The only thing that *I* can think of is that it would be difficult to know
whether/when you were supposed to be dealing with a <valuable>, and so it would
tend to lead to a programming style where you end up sending #value to
*everything*, just in case.


> Please excuse me if this was best posted to c.l.s instead, it's just that
> it's so much quieter and sane in here...

I don't blame you ;-)

Just for fun, the following filein is rather weird (and largely untested) hack
that gets a similar effect by going the other way around, allowing a block to
be used in place of a "normal" value.  It's a "proxy"-style class that wraps a
<valuable> and uses DNU handling to evaluate the <valuable>, and forward every
message to the result.  E.g:

dict := (LookupTable new)
           at: #StaticNow put: Time now;
           at: #DynamicNow put: (ValuableAsValue for: [Time now]);
           yourself.

dict at: #StaticNow.  "--> 10:38:56"
dict at: #DynamicNow. "--> 10:38:57"
"later..."
dict at: #StaticNow.  "--> 10:38:56"
dict at: #DynamicNow. "--> 10:39:06"

etc ;-)

    -- chris

==========
"Filed out from Dolphin Smalltalk XP"!

ProtoObject subclass: #ValuableAsValue
 instanceVariableNames: 'valuable'
 classVariableNames: ''
 poolDictionaries: ''
 classInstanceVariableNames: ''!
ValuableAsValue guid: (GUID fromString:
'{C0957A5B-CD10-4A57-B904-FF490E77EFFD}')!
ValuableAsValue comment: ''!
!ValuableAsValue categoriesForClass!Unclassified! !
!ValuableAsValue methodsFor!

__valuable
 "answer the <niladicValuable> that we wrap"

 ^ valuable.!

__valuable: a0Block
 "private -- set the <niladicValuable> that we wrap"

 valuable := a0Block.!

basicClass

 <primitive: 111>
 ^self primitiveFailed!

debugPrintString
 "version of #printString for use in the debugger and similar contexts"

 ^ (String writeStream)
  display: self class;
  nextPutAll: ' for: ';
  print: valuable;
  contents.!

doesNotUnderstand: aMessage
 "handler for messages (which should be all of them) that we don't understand.
 We forward to the result of evaluating our <niladicValuable>, thus (sort of)
 allowing a block to act is lieu of a *normal* object"

 ^ aMessage forwardTo: (valuable value).! !
!ValuableAsValue categoriesFor: #__valuable!accessing!public! !
!ValuableAsValue categoriesFor: #__valuable:!initialization!private! !
!ValuableAsValue categoriesFor: #basicClass!accessing!public! !
!ValuableAsValue categoriesFor: #debugPrintString!printing!public! !
!ValuableAsValue categoriesFor: #doesNotUnderstand:!message dispatching!public!
!

!ValuableAsValue class methodsFor!

for: a0Block
 "answer an instance that wraps the <niladicValuable>, a0Block,
 and uses DNU handling to forward all messages (except __valuable[:],
 #basicClass, and #debugPrintString) to the result of evaluating that valuable"

 | new |

 new := self basicNew.
 new __valuable: a0Block.

 ^ new.

  ! !
!ValuableAsValue class categoriesFor: #for:!public! !
===========