SmartRefStream broken?

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

SmartRefStream broken?

Aaron Reichow
Ahoy!

We were having a discussion on #squeak about SqueakMap, older images,  
and SmartRefStream. Eventually, we were sharing snippets of code. We  
were trying to do the following:

"Save the data"
| data rr |
data := Dictionary new.
data at: #Meef put: 25;
         at: 23 put: 'Amanda';
         at: 'Small Numbers' put: #(0 1 2 3 four).
rr := SmartRefStream fileNamed: 'data.obj'.
rr nextPut: data; close.

"Restore it!"
| restoredData rr |
rr := SmartRefStream fileNamed: 'data.obj'.
restoredData := rr next.
restoredData inspect.
rr close.

This doesn't work. You can save the serialized object, but when you  
try to run the restore snippit, you get a walkback. One note- it does  
work just fine when you replace SmartRefStream with ReferenceStream -  
that works fine on both the saving and the restoring.  All examples  
of serializing objects I can find involve ReferenceStream or an  
ImageSegment, nothing with the SRS.  That said, SmartRefStream has  
some features that are useful and we'd like to use.

I won't put the whole walk back here- the code will bring up this  
error in any image. I tried the following images-

2.2: doesn't exist there.
2.4, 2.8, 3.2, 3.4, 3.8: the same error.

My first question- are we using it wrong?  Any other ideas?

Regards,
Aaron

Reply | Threaded
Open this post in threaded view
|

Re: SmartRefStream broken?

Jon Hylands
On Tue, 14 Nov 2006 14:40:46 -0600, Aaron Reichow <[hidden email]>
wrote:

> My first question- are we using it wrong?  Any other ideas?

The way I use them is this:

(writing) - same as you do
(reading)

        | file object |
        file := FileStream readOnlyFileNamed: 'data.obj'.
        object := file fileInObjectAndCode.
        file close.
        ^object

It looks weird, but it works...

Later,
Jon

--------------------------------------------------------------
   Jon Hylands      [hidden email]      http://www.huv.com/jon

  Project: Micro Seeker (Micro Autonomous Underwater Vehicle)
           http://www.huv.com

Reply | Threaded
Open this post in threaded view
|

Re: SmartRefStream broken?

Benjamin Pollack
On 11/14/06, Jon Hylands <[hidden email]> wrote:
The way I use them is this:

(writing) - same as you do
(reading)

        | file object |
        file := FileStream readOnlyFileNamed: 'data.obj'.
        object := file fileInObjectAndCode.
        file close.
        ^object

Is there a reason why SmartRefStream has such a radically different interface than ReferenceStream, even though it's a direct subclass? That seems really misleading. At the very least, the class comment should say explicitly, right at the top, that SmartRefStream should only be used via FileStream, but to be honest that's just a superficial band-aid over a horrible design bug. Either the class should be renamed and broken out of the hierarchy or it should obey the same rules as its parent class.


Reply | Threaded
Open this post in threaded view
|

Re: SmartRefStream broken?

Andreas.Raab
Benjamin Pollack wrote:
> Is there a reason why SmartRefStream has such a radically different
> interface than ReferenceStream, even though it's a direct subclass?

Yes. The reason is that you shouldn't use SmartRefStream the way you're
using it. The "correct" way is using #fileOutClass:andObject: like here:

Writing:
   file := FileStream newFileNamed: 'data.obj'.
   file fileOutClass: nil "no class" andObject: data.

Reading:
   file := FileStream readOnlyFileNamed: 'data.obj'.
   data := file fileInObjectAndCode.

Cheers,
   - Andreas

Reply | Threaded
Open this post in threaded view
|

Re: SmartRefStream broken?

Aaron Reichow
In reply to this post by Jon Hylands
Jon-

On Nov 14, 2006, at 2:47 PM, Jon Hylands wrote:

> The way I use them is this:
>
> (writing) - same as you do
> (reading)
>
> | file object |
> file := FileStream readOnlyFileNamed: 'data.obj'.
> object := file fileInObjectAndCode.
> file close.
> ^object

Hrmm... that seems to work fine, #fileInObjectAndCode is actually  
using SmartRefStream.  Seems to be an pretty messy design- the only  
difference is that #fileInObjectAndCode uses:

SmartRefStream on: file contents.

instead of

SmartRefStream fileNamed: ...

Thanks for the pointer, Jon- I don't know if I would've seen that  
otherwise!  It would be easy to fix SmartRefStream, but what is in  
there now is pretty inconsistent... but at least it works, if you say  
the right magic words.

Regards,
Aaron


Reply | Threaded
Open this post in threaded view
|

Re: SmartRefStream broken?

Aaron Reichow
In reply to this post by Andreas.Raab

On Nov 14, 2006, at 4:02 PM, Andreas Raab wrote:

> Benjamin Pollack wrote:
>> Is there a reason why SmartRefStream has such a radically  
>> different interface than ReferenceStream, even though it's a  
>> direct subclass?
>
> Yes. The reason is that you shouldn't use SmartRefStream the way  
> you're using it. The "correct" way is using  
> #fileOutClass:andObject: like here:

That might be preferred way to use it, but it isn't any excuse for  
the problem- poor design.  What works with ReferenceStream doesn't  
work with its subclass, SmartRefStream. Regardless of what might be  
preferred, it isn't unnatural to take what works fine with  
ReferenceStream and try to apply that to SmartRefStream, Many of the  
examples I've seen throughout the image and other applications (e.g.  
ScriptManager) use it in the way I wrote in my original post.  No  
excuse for it, but at least it's easily fixed!

Thanks Andreas!

Regards,
Aaron


Reply | Threaded
Open this post in threaded view
|

Re: SmartRefStream broken?

Lex Spoon
Aaron Reichow <[hidden email]> writes:

> On Nov 14, 2006, at 4:02 PM, Andreas Raab wrote:
>
> > Benjamin Pollack wrote:
> >> Is there a reason why SmartRefStream has such a radically
> >> different interface than ReferenceStream, even though it's a
> >> direct subclass?
> >
> > Yes. The reason is that you shouldn't use SmartRefStream the way
> > you're using it. The "correct" way is using
> > #fileOutClass:andObject: like here:
>
> That might be preferred way to use it, but it isn't any excuse for
> the problem- poor design.  What works with ReferenceStream doesn't
> work with its subclass, SmartRefStream. Regardless of what might be
> preferred, it isn't unnatural to take what works fine with
> ReferenceStream and try to apply that to SmartRefStream, Many of the
> examples I've seen throughout the image and other applications (e.g.
> ScriptManager) use it in the way I wrote in my original post.  No
> excuse for it, but at least it's easily fixed!

I have always used the mixed style that Benjamin describes: use
SmartRefStream directly for fileout, and use fileInObjectAndCode for
fileIn.  I remember this incantation being tricky to figure out.


I agree that at the least it would really help if the documentation
within class SmartRefStream were updated to include common use cases.
Right now the class comment is huge but it does not include basics
like (a) when do you use it and (b) what is the standard idiom (which
I guess is what Andreas wrote).  Are there any takers from this thread
to write a little comment-adding changeset?


-Lex