Monday, February 05, 2007

"Iterators are not intended to be serializable." Huh?

In a recent attempt to reduce my "Unread" mailbox, I went back in it marking as "Read" everything that has been, well, read. So it happened that I stumbled across a bug report I filed with Sun around 7 months ago (yes, that mailbox was deep), and went to see what became of it. You can see it here.
The problem I reported is that various iterator objects shipped with Sun's JRE aren't serializable. Looking at the bug's page, I noticed that the "Evaluation" field has been filled out with:

Iterators are not intended to be serializable. Probably.... Not a Defect.
Wow. Whoever evaluated this wasn't particularly, to put it mildly, in short supply of short-sightedness. In accordance to this, the bug was marked as "Request for Enhancement".
Now please tell me if I see it wrong, and if there never-ever can arise a valid class design where a stateful object maintains certain aspect of its state by keeping an iterator into a collection, and said object should be serializable. At least for lists, the concept can even be emulated by holding a reference to the list object and an index variable, so it definitely doesn't sound like a very unsane idea.
I will admit though that I didn't come across an explicit case of it myself. How did I stumble upon this bug then? Continuations. I had a JavaScript program that had a loop going through a collection with an iterator, and captured a continuation within the loop and subsequently attempted to serialize the continuation. Naturally, the continuation being within a loop, the serialization graph of objects reachable through it contained the iterator. Blam.
Now, this admittedly might sound as a more esoteric case of serializing state that contains an iterator, but really, were I to use a "for" loop with explicit indexing instead of an iterator, it wouldn't have been a problem. Therefore, I can't see serializing an iterator to be such an off-the-wall idea to justify the anonymous Sun mouthpiece saying "Iterators are not intended to be serializable"?

3 comments:

Cs said...

Still better that my experience, when I filed a bug report for MiKTeX which was Hungarian language related. The answer was: "it is a bug, but it will not be fixed". Nevertheless, they fixed it later. Maybe it was just an effect of something more serious.

John Rusk said...

Hi,

I've just put together a solution which serializes iterator state in C#. I thought you might be interested because (a) it demonstrates another reason _why_ someone might want to so such a thing (in spite of Sun's assumption that no-one would want to) and (b) you might be interested in how it can be done in .NET, by leveraging the extension hooks in .NET's serialization mechanism.

Details are here, including source code: http://dotnet.agilekiwi.com/blog/2007/05/implementing-workflow-with-persistent.html

Richard said...

I had a straightforward case of this. The iterator was recording a flight's progress through a series of waypoints - the iterator effectively kept track of the last waypoint passed.

A workaround which was straightforward in this case was to replace the iterator with an int index, which is Serializable.