0

I'm playing with Pickled Scala signatures, writing bytes to a PickleBuffer and reading them back out with ShowPickled.

I can write and read back an Int as expected by using

PB.myBuf.writeByte(2)

giving the '2' (a reference to an entry number in this case) in

1(MyRecord) 2 40[case] 5.

But if I but that same code inside a conditional like:

if (ExtModClassRef.position==0) PB.myBuf.writeByte(2),

or

ExtModClassRef.position match {case 0 => PB.myBuf.writeByte(2)},

then the bytes that I read back are garbled:

0,4: CLASSsym 4: 1(MyRecordmodels\00\00...

instead of how the first few entries should be:

0,4: CLASSsym 4: 1(MyRecord) 2 40[case] 5 1,10: TYPEname 8: MyRecord 2,20: EXTMODCLASSref 1: 3(models) 3,23: TERMname 6: models

I'm puzzled as to how the if and match keywords could have any effect on what bytes are written.

And perhaps someone can suggest a fix or a workaround?

Thank you, Julian

Julian Peeters
  • 853
  • 1
  • 6
  • 19

1 Answers1

2

The only two ways I can see to influence what bytes are written are:

  1. ExtModClassRef.position actually isn't 0, so the bytes you expect to be written aren't, and when you write something else later, it corrupts the record.

  2. ExtModClassRef.position call modifies myBuf in some way.

It's impossible to determine which is the case (or perhaps something different) without seeing your own code.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • Whoops, it was #2. I had thought that I could just "peek" at the object's member without instantiate it. Solved by encapsulating some of the objects code in `def`. Thanks very, very much for taking the time -Julian – Julian Peeters Aug 05 '13 at 23:49
  • Ha, also +1. I also thought side-effect, but I couldn't take the time, so +1 for taking the time, as @JulianPeeters says. – som-snytt Aug 06 '13 at 05:46