This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Continuation behavior

3 replies
Meredith Gregory
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Dear Continually Scala,
Here's a little snippet of code that captures continuations
  • as an in-memory representation, and
  • as a serialized representation
The two different captures give different behaviors because they capture different environments. Here's a trace to illustrate:
Welcome to Scala version 2.9.0.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29). Type in expressions to have them evaluated.Type :help for more information.
scala> :load /Users/lgm/work/src/projex/stellar/mdp4tw/SpecialK.SDEC/scripts/k.scalacala Loading /Users/lgm/work/src/projex/stellar/mdp4tw/SpecialK.SDEC/scripts/k.scala...import scala.collection.mutable.HashMap import scala.util.continuations._import com.thoughtworks.xstream.XStreamimport com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver import biz.source_code.base64Coder.Base64Coderimport java.io.ObjectInputStreamimport java.io.ByteArrayInputStream import java.io.ObjectOutputStreamimport java.io.ByteArrayOutputStreamdefined module ObjectToString defined module OK
scala> val ksmap1 = new HashMap[String,String]( )   val ksmap1 = new HashMap[String,String]( )  ksmap1: scala.collection.mutable.HashMap[String,String] = Map()
scala> val kmap1 = new HashMap[String,(String => Unit)]( )  val kmap1 = new HashMap[String,(String => Unit)]( )   kmap1: scala.collection.mutable.HashMap[String,(String) => Unit] = Map()
scala> OK.coral( false )( kmap1, ksmap1, "Erp" ) OK.coral( false )( kmap1, ksmap1, "Erp" )continuation as string: rO0ABXNyAEdzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHQkJGFub25mdW4kbWFwJDIkJGFub25mdW4kYXBwbHkkMQAAAAAAAAAAAgADTAAGJG91dGVydAA4THNjYWxhL3V0aWwvY29udGludWF0aW9ucy9Db250cm9sQ29udGV4dCQkYW5vbmZ1biRtYXAkMjtMAANrJDF0ABFMc2NhbGEvRnVuY3Rpb24xO0wABXRociQxcQB+AAJ4cHNyADZzY2FsYS51dGlsLmNvbnRpbnVhdGlvb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FsYS51dGlsLmNvbnRpbnVhdGlvbnMucGFja2FnZSQkYW5vbmZ1biRyZXNldCQxAAAAAAAAAAACAAB4cHNyADpzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHQkJGFub25mdW4kZm9yZWFjaCQxAAAAAAAAAAACAAB4cA== resuming...searching kmap : Map(Erp -> <function1>)fns is <function1>
scala> kmap1( "Erp" )( "Erp" )kmap1( "Erp" )( "Erp" ) resuming...searching kmap : Map(Erp -> <function1>)fns is <function1>
scala> val rK1 = ObjectToString.fromString( ksmap1( "Erp" ) ).asInstanceOf[( String => Unit )] ing => Unit )]rK1: (String) => Unit = <function1>
scala> rK1( "Erp" )rK1( "Erp" )resuming... searching kmap : Map()
scala> 
What is the intended semantics? Should they capture the same environments? Should the two executions be the same?
Best wishes in the New Year,
--greg
P.S. As a side note, i used to be able to serialize/deserialize delimited continuations using XStream. This fails now with
scala> val rK1 = new XStream( new JettisonMappedXmlDriver() ).fromXML( ks1 ) val rK1 = new XStream( new JettisonMappedXmlDriver() ).fromXML( ks1 )com.thoughtworks.xstream.converters.ConversionException: Element fun of type scala.util.continuations.package$$anonfun$shiftR$1 is not defined as field in type scala.util.continuations.ControlContext$$anonfun$map$2 ---- Debugging information ----class               : scala.util.continuations.ControlContext$$anonfun$map$2$$anonfun$apply$1 required-type       : scala.util.continuations.ControlContext$$anonfun$map$2path                : /scala.util.continuations.ControlContext$$anonfun$map$2$$anonfun$apply$1/$outer[2]/fun line number         : -1------------------------------- at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.writeValueToImplicitCollection(AbstractReflectionConverter.java:283) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:236) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:162) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:246) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:218) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:162) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:60) at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:137) at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:33) at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:923) at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:909) at com.thoughtworks.xstream.XStream.fromXML(XStream.java:853) at com.thoughtworks.xstream.XStream.fromXML(XStream.java:845) at .<init>(<console>:36) at .<clinit>(<console>) at .<init>(<console>:11) at .<clinit>(<console>) at $export(<console>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:592) at scala.tools.nsc.interpreter.IMain$Request$$anonfun$10.apply(IMain.scala:828) at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43) at scala.tools.nsc.io.package$$anon$2.run(package.scala:31) at java.lang.Thread.run(Thread.java:680)
scala> 
--
L.G. Meredith
Managing Partner
Biosimilarity LLC
7329 39th Ave SWSeattle, WA 98136

+1 206.650.3740

http://biosimilarity.blogspot.com
jamesearldouglas
Joined: 2011-03-28,
User offline. Last seen 1 year 31 weeks ago.
Re: Continuation behavior

> ksmap += ( ( key, ks ) )
> kmap += ( ( key, k ) )

It looks like when you add ks to ksmap it represents a closure around
k, so the subsequent addition to kmap is not included. If you add a
line to the continuation to print out the object reference id for
kmap, you'll see it is different for the two environments:

println( "resuming..." )
println( "searching kmap : " + kmap )
println( "kmap id : " + System.identityHashCode(kmap)) // add
this line

scala> val ksmap1 = new HashMap[String,String]( )
ksmap1: scala.collection.mutable.HashMap[String,String] = Map()

scala> val kmap1 = new HashMap[String,(String => Unit)]( )
kmap1: scala.collection.mutable.HashMap[String,String => Unit] = Map()

scala> OK.coral( false )( kmap1, ksmap1, "Erp" )
continuation as string:
rO0ABXNyAEdzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHQkJGFub25mdW4kbWFwJDIkJGFub25mdW4kYXBwbHkkMQAAAAAAAAAAAgADTAAGJG91dGVydAA4THNjYWxhL3V0aWwvY29udGludWF0aW9ucy9Db250cm9sQ29udGV4dCQkYW5vbmZ1biRtYXAkMjtMAANrJDF0ABFMc2NhbGEvRnVuY3Rpb24xO0wABXRociQxcQB
+AAJ4cHNyADZzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHQkJGFub25mdW4kbWFwJDIAAAAAAAAAAAIAAkwABiRvdXRlcnQAKUxzY2FsYS91dGlsL2NvbnRpbnVhdGlvbnMvQ29udHJvbENvbnRleHQ7TAADZiQxcQB
+AAJ4cHNyACdzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHTbw
+rG97imMwIAAkwAA2Z1bnQAEUxzY2FsYS9GdW5jdGlvbjI7TAABeHQAEkxqYXZhL2xhb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yQkaXckJGl3JCRpdyQkaXckJGl3JCRpdyQkaXckJGl3JCRpdyQkaXckJGl3JCRpdyQkaXckJGl3JCRpdyQkaXckJGl3JCRpdyQkaXckJGl3JE9LJCRhbm9uZnVuJGNvcmFsJDEkJGFub25mdW4kYXBwbHkkMgAAAAAAAAAAAgABTAAGJG91dGVycQB
+AA54cHEAfgATc3IAMXNjYWxhLnV0aWwuY29udGludWF0aW9ucy5wYWNrYWdlJCRhbm9uZnVuJHJlc2V0JDEAAAAAAAAAAAIAAHhwc3IAOnNjYWxhLnV0aWwuY29udGludWF0aW9ucy5Db250cm9sQ29udGV4dCQkYW5vbmZ1biRmb3JlYWNoJDEAAAAAAAAAAAIAAHhw
resuming...
searching kmap : Map(Erp -> )
kmap id : 24530783
fns is

scala> kmap1( "Erp" )( "Erp" )
resuming...
searching kmap : Map(Erp -> )
kmap id : 24530783
fns is

scala> val rK1 = ObjectToString.fromString( ksmap1( "Erp" )
| ).asInstanceOf[( String => Unit )]
rK1: String => Unit =

scala> rK1( "Erp" )
resuming...
searching kmap : Map()
kmap id : 4430367

On Dec 29, 4:29 pm, Meredith Gregory wrote:
> Dear Continually Scala,
>
> Here's a little snippet of code that
> captures continuations
>
>    - as an in-memory representation, and
>    - as a serialized representation
>
> The two different captures give different behaviors because they capture
> different environments. Here's a trace to illustrate:
>
> Welcome to Scala version 2.9.0.1 (Java HotSpot(TM) 64-Bit Server VM, Java
> 1.6.0_29).
> Type in expressions to have them evaluated.
> Type :help for more information.
>
> scala> :load
> /Users/lgm/work/src/projex/stellar/mdp4tw/SpecialK.SDEC/scripts/k.scala
> cala
> Loading
> /Users/lgm/work/src/projex/stellar/mdp4tw/SpecialK.SDEC/scripts/k.scala...
> import scala.collection.mutable.HashMap
> import scala.util.continuations._
> import com.thoughtworks.xstream.XStream
> import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver
> import biz.source_code.base64Coder.Base64Coder
> import java.io.ObjectInputStream
> import java.io.ByteArrayInputStream
> import java.io.ObjectOutputStream
> import java.io.ByteArrayOutputStream
> defined module ObjectToString
> defined module OK
>
> scala> val ksmap1 = new HashMap[String,String]( )
> val ksmap1 = new HashMap[String,String]( )
> ksmap1: scala.collection.mutable.HashMap[String,String] = Map()
>
> scala> val kmap1 = new HashMap[String,(String => Unit)]( )
> val kmap1 = new HashMap[String,(String => Unit)]( )
> kmap1: scala.collection.mutable.HashMap[String,(String) => Unit] = Map()
>
> scala> OK.coral( false )( kmap1, ksmap1, "Erp" )
> OK.coral( false )( kmap1, ksmap1, "Erp" )
> continuation as string:
> rO0ABXNyAEdzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHQkJGFub25mdW4 kbWFwJDIkJGFub25mdW4kYXBwbHkkMQAAAAAAAAAAAgADTAAGJG91dGVydAA4THNjYWxhL3V0aW wvY29udGludWF0aW9ucy9Db250cm9sQ29udGV4dCQkYW5vbmZ1biRtYXAkMjtMAANrJDF0ABFMc 2NhbGEvRnVuY3Rpb24xO0wABXRociQxcQB+AAJ4cHNyADZzY2FsYS51dGlsLmNvbnRpbnVhdGlv bnMuQ29udHJvbENvbnRleHQkJGFub25mdW4kbWFwJDIAAAAAAAAAAAIAAkwABiRvdXRlcnQAKUx zY2FsYS91dGlsL2NvbnRpbnVhdGlvbnMvQ29udHJvbENvbnRleHQ7TAADZiQxcQB+AAJ4cHNyAC dzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHTbw+rG97imMwIAAkwAA2Z1b nQAEUxzY2FsYS9GdW5jdGlvbjI7TAABeHQAEkxqYXZhL2xhbmcvT2JqZWN0O3hwc3IAMnNjYWxh LnV0aWwuY29udGludWF0aW9ucy5wYWNrYWdlJCRhbm9uZnVuJHNoaWZ0UiQxAAAAAAAAAAACAAF MAAVmdW4kMXEAfgACeHBzcgBCJGxpbmUxMS4kcmVhZCQkaXckJGl3JCRpdyQkaXckT0skJGFub2 5mdW4kY29yYWwkMSQkYW5vbmZ1biRhcHBseSQxAAAAAAAAAAACAAFMAAYkb3V0ZXJ0ADNMJGxpb mUxMS8kcmVhZCQkaXckJGl3JCRpdyQkaXckT0skJGFub25mdW4kY29yYWwkMTt4cHNyADEkbGlu ZTExLiRyZWFkJCRpdyQkaXckJGl3JCRpdyRPSyQkYW5vbmZ1biRjb3JhbCQxAAAAAAAAAAACAAR aAAx1c2VYU3RyZWFtJDFMAAVrZXkkMXQAEkxqYXZhL2xhbmcvU3RyaW5nO0wABmttYXAkMXQAIk xzY2FsYS9jb2xsZWN0aW9uL211dGFibGUvSGFzaE1hcDtMAAdrc21hcCQxcQB+ABJ4cAB0AANFc nBzcgAgc2NhbGEuY29sbGVjdGlvbi5tdXRhYmxlLkhhc2hNYXAAAAAAAAAAAQMAAHhwdwkAAALu AAAAAAB4c3EAfgAVdwkAAALuAAAAAAB4cHNyAEIkbGluZTExLiRyZWFkJCRpdyQkaXckJGl3JCR pdyRPSyQkYW5vbmZ1biRjb3JhbCQxJCRhbm9uZnVuJGFwcGx5JDIAAAAAAAAAAAIAAUwABiRvdX RlcnEAfgAOeHBxAH4AE3NyADFzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMucGFja2FnZSQkYW5vb mZ1biRyZXNldCQxAAAAAAAAAAACAAB4cHNyADpzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29u dHJvbENvbnRleHQkJGFub25mdW4kZm9yZWFjaCQxAAAAAAAAAAACAAB4cA==
> resuming...
> searching kmap : Map(Erp -> )
> fns is
>
> scala> kmap1( "Erp" )( "Erp" )
> kmap1( "Erp" )( "Erp" )
> resuming...
> searching kmap : Map(Erp -> )
> fns is
>
> scala> val rK1 = ObjectToString.fromString( ksmap1( "Erp" )
> ).asInstanceOf[( String => Unit )]
> ing => Unit )]
> rK1: (String) => Unit =
>
> scala> rK1( "Erp" )
> rK1( "Erp" )
> resuming...
> searching kmap : Map()
>
> scala>
>
> What is the intended semantics? Should they capture the same environments?
> Should the two executions be the same?
>
> Best wishes in the New Year,
>
> --greg
>
> P.S. As a side note, i used to be able to serialize/deserialize delimited
> continuations using XStream. This fails now with
>
> scala> val rK1 = new XStream( new JettisonMappedXmlDriver() ).fromXML( ks1 )
> val rK1 = new XStream( new JettisonMappedXmlDriver() ).fromXML( ks1 )
> com.thoughtworks.xstream.converters.ConversionException: Element fun of
> type scala.util.continuations.package$$anonfun$shiftR$1 is not defined as
> field in type scala.util.continuations.ControlContext$$anonfun$map$2
> ---- Debugging information ----
> class               :
> scala.util.continuations.ControlContext$$anonfun$map$2$$anonfun$apply$1
> required-type       : scala.util.continuations.ControlContext$$anonfun$map$2
> path                :
> /scala.util.continuations.ControlContext$$anonfun$map$2$$anonfun$apply$1/$o uter[2]/fun
> line number         : -1
> -------------------------------
> at
> com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. writeValueToImplicitCollection(AbstractReflectionConverter.java:283)
> at
> com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. doUnmarshal(AbstractReflectionConverter.java:236)
> at
> com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. unmarshal(AbstractReflectionConverter.java:162)
> at
> com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.jav a:82)
> at
> com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(Abstrac tReferenceUnmarshaller.java:63)
> at
> com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshal ler.java:76)
> at
> com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. unmarshallField(AbstractReflectionConverter.java:246)
> at
> com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. doUnmarshal(AbstractReflectionConverter.java:218)
> at
> com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. unmarshal(AbstractReflectionConverter.java:162)
> at
> com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.jav a:82)
> at
> com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(Abstrac tReferenceUnmarshaller.java:63)
> at
> com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshal ler.java:76)
> at
> com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshal ler.java:60)
> at
> com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java: 137)
> at
> com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(Abs tractTreeMarshallingStrategy.java:33)
> at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:923)
> at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:909)
> at com.thoughtworks.xstream.XStream.fromXML(XStream.java:853)
> at com.thoughtworks.xstream.XStream.fromXML(XStream.java:845)
> at .(:36)
> at .()
> at .(:11)
> at .()
> at $export()
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:3 9)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImp l.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:592)
> at
> scala.tools.nsc.interpreter.IMain$Request$$anonfun$10.apply(IMain.scala:828 )
> at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
> at scala.tools.nsc.io.package$$anon$2.run(package.scala:31)
> at java.lang.Thread.run(Thread.java:680)
>
> scala>
>
> --
> L.G. Meredith
> Managing Partner
> Biosimilarity LLC
> 7329 39th Ave SW
> Seattle, WA 98136
>
> +1 206.650.3740
>
> http://biosimilarity.blogspot.com

jamesearldouglas
Joined: 2011-03-28,
User offline. Last seen 1 year 31 weeks ago.
Re: Continuation behavior

Please pardon the mangled text. I put the same source and REPL output
here: https://gist.github.com/1541001

On Dec 30, 10:49 am, James Douglas wrote:
> > ksmap += ( ( key, ks ) )
> > kmap += ( ( key, k ) )
>
> It looks like when you add ks to ksmap it represents a closure around
> k, so the subsequent addition to kmap is not included.  If you add a
> line to the continuation to print out the object reference id for
> kmap, you'll see it is different for the two environments:
>
>       println( "resuming..." )
>       println( "searching kmap : " + kmap )
>       println( "kmap id : " + System.identityHashCode(kmap)) // add
> this line
>
> scala> val ksmap1 = new HashMap[String,String]( )
> ksmap1: scala.collection.mutable.HashMap[String,String] = Map()
>
> scala> val kmap1 = new HashMap[String,(String => Unit)]( )
> kmap1: scala.collection.mutable.HashMap[String,String => Unit] = Map()
>
> scala> OK.coral( false )( kmap1, ksmap1, "Erp" )
> continuation as string:
> rO0ABXNyAEdzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHQkJGFub25mdW4 kbWFwJDIkJGFub25mdW4kYXBwbHkkMQAAAAAAAAAAAgADTAAGJG91dGVydAA4THNjYWxhL3V0aW wvY29udGludWF0aW9ucy9Db250cm9sQ29udGV4dCQkYW5vbmZ1biRtYXAkMjtMAANrJDF0ABFMc 2NhbGEvRnVuY3Rpb24xO0wABXRociQxcQB
> +AAJ4cHNyADZzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHQkJGFub25mdW 4kbWFwJDIAAAAAAAAAAAIAAkwABiRvdXRlcnQAKUxzY2FsYS91dGlsL2NvbnRpbnVhdGlvbnMvQ 29udHJvbENvbnRleHQ7TAADZiQxcQB
> +AAJ4cHNyACdzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHTbw
> +rG97imMwIAAkwAA2Z1bnQAEUxzY2FsYS9GdW5jdGlvbjI7TAABeHQAEkxqYXZhL2xhbmcvT2Jq ZWN0O3hwc3IAMnNjYWxhLnV0aWwuY29udGludWF0aW9ucy5wYWNrYWdlJCRhbm9uZnVuJHNoaWZ 0UiQxAAAAAAAAAAACAAFMAAVmdW4kMXEAfgACeHBzcgCbJGxpbmUxNzMuJHJlYWQkJGl3JCRpdy QkaXckJGl3JCRpdyQkaXckJGl3JCRpdyQkaXckJ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gATc3IAMXNjYWxhLnV0aWwuY29udGludWF0aW9ucy5wYWNrYWdlJCRhbm9uZnVuJH Jlc2V0JDEAAAAAAAAAAAIAAHhwc3IAOnNjYWxhLnV0aWwuY29udGludWF0aW9ucy5Db250cm9sQ 29udGV4dCQkYW5vbmZ1biRmb3JlYWNoJDEAAAAAAAAAAAIAAHhw
> resuming...
> searching kmap : Map(Erp -> )
> kmap id : 24530783
> fns is
>
> scala> kmap1( "Erp" )( "Erp" )
> resuming...
> searching kmap : Map(Erp -> )
> kmap id : 24530783
> fns is
>
> scala> val rK1 = ObjectToString.fromString( ksmap1( "Erp" )
>      | ).asInstanceOf[( String => Unit )]
> rK1: String => Unit =
>
> scala> rK1( "Erp" )
> resuming...
> searching kmap : Map()
> kmap id : 4430367
>
> On Dec 29, 4:29 pm, Meredith Gregory wrote:
>
>
>
>
>
>
>
> > Dear Continually Scala,
>
> > Here's a little snippet of code that
> > captures continuations
>
> >    - as an in-memory representation, and
> >    - as a serialized representation
>
> > The two different captures give different behaviors because they capture
> > different environments. Here's a trace to illustrate:
>
> > Welcome to Scala version 2.9.0.1 (Java HotSpot(TM) 64-Bit Server VM, Java
> > 1.6.0_29).
> > Type in expressions to have them evaluated.
> > Type :help for more information.
>
> > scala> :load
> > /Users/lgm/work/src/projex/stellar/mdp4tw/SpecialK.SDEC/scripts/k.scala
> > cala
> > Loading
> > /Users/lgm/work/src/projex/stellar/mdp4tw/SpecialK.SDEC/scripts/k.scala...
> > import scala.collection.mutable.HashMap
> > import scala.util.continuations._
> > import com.thoughtworks.xstream.XStream
> > import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver
> > import biz.source_code.base64Coder.Base64Coder
> > import java.io.ObjectInputStream
> > import java.io.ByteArrayInputStream
> > import java.io.ObjectOutputStream
> > import java.io.ByteArrayOutputStream
> > defined module ObjectToString
> > defined module OK
>
> > scala> val ksmap1 = new HashMap[String,String]( )
> > val ksmap1 = new HashMap[String,String]( )
> > ksmap1: scala.collection.mutable.HashMap[String,String] = Map()
>
> > scala> val kmap1 = new HashMap[String,(String => Unit)]( )
> > val kmap1 = new HashMap[String,(String => Unit)]( )
> > kmap1: scala.collection.mutable.HashMap[String,(String) => Unit] = Map()
>
> > scala> OK.coral( false )( kmap1, ksmap1, "Erp" )
> > OK.coral( false )( kmap1, ksmap1, "Erp" )
> > continuation as string:
> > rO0ABXNyAEdzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHQkJGFub25mdW4 kbWFwJDIkJGFub25mdW4kYXBwbHkkMQAAAAAAAAAAAgADTAAGJG91dGVydAA4THNjYWxhL3V0aW wvY29udGludWF0aW9ucy9Db250cm9sQ29udGV4dCQkYW5vbmZ1biRtYXAkMjtMAANrJDF0ABFMc 2NhbGEvRnVuY3Rpb24xO0wABXRociQxcQB+AAJ4cHNyADZzY2FsYS51dGlsLmNvbnRpbnVhdGlv bnMuQ29udHJvbENvbnRleHQkJGFub25mdW4kbWFwJDIAAAAAAAAAAAIAAkwABiRvdXRlcnQAKUx zY2FsYS91dGlsL2NvbnRpbnVhdGlvbnMvQ29udHJvbENvbnRleHQ7TAADZiQxcQB+AAJ4cHNyAC dzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHTbw+rG97imMwIAAkwAA2Z1b nQAEUxzY2FsYS9GdW5jdGlvbjI7TAABeHQAEkxqYXZhL2xhbmcvT2JqZWN0O3hwc3IAMnNjYWxh LnV0aWwuY29udGludWF0aW9ucy5wYWNrYWdlJCRhbm9uZnVuJHNoaWZ0UiQxAAAAAAAAAAACAAF MAAVmdW4kMXEAfgACeHBzcgBCJGxpbmUxMS4kcmVhZCQkaXckJGl3JCRpdyQkaXckT0skJGFub2 5mdW4kY29yYWwkMSQkYW5vbmZ1biRhcHBseSQxAAAAAAAAAAACAAFMAAYkb3V0ZXJ0ADNMJGxpb mUxMS8kcmVhZCQkaXckJGl3JCRpdyQkaXckT0skJGFub25mdW4kY29yYWwkMTt4cHNyADEkbGlu ZTExLiRyZWFkJCRpdyQkaXckJGl3JCRpdyRPSyQkYW5vbmZ1biRjb3JhbCQxAAAAAAAAAAACAAR aAAx1c2VYU3RyZWFtJDFMAAVrZXkkMXQAEkxqYXZhL2xhbmcvU3RyaW5nO0wABmttYXAkMXQAIk xzY2FsYS9jb2xsZWN0aW9uL211dGFibGUvSGFzaE1hcDtMAAdrc21hcCQxcQB+ABJ4cAB0AANFc nBzcgAgc2NhbGEuY29sbGVjdGlvbi5tdXRhYmxlLkhhc2hNYXAAAAAAAAAAAQMAAHhwdwkAAALu AAAAAAB4c3EAfgAVdwkAAALuAAAAAAB4cHNyAEIkbGluZTExLiRyZWFkJCRpdyQkaXckJGl3JCR pdyRPSyQkYW5vbmZ1biRjb3JhbCQxJCRhbm9uZnVuJGFwcGx5JDIAAAAAAAAAAAIAAUwABiRvdX RlcnEAfgAOeHBxAH4AE3NyADFzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMucGFja2FnZSQkYW5vb mZ1biRyZXNldCQxAAAAAAAAAAACAAB4cHNyADpzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29u dHJvbENvbnRleHQkJGFub25mdW4kZm9yZWFjaCQxAAAAAAAAAAACAAB4cA==
> > resuming...
> > searching kmap : Map(Erp -> )
> > fns is
>
> > scala> kmap1( "Erp" )( "Erp" )
> > kmap1( "Erp" )( "Erp" )
> > resuming...
> > searching kmap : Map(Erp -> )
> > fns is
>
> > scala> val rK1 = ObjectToString.fromString( ksmap1( "Erp" )
> > ).asInstanceOf[( String => Unit )]
> > ing => Unit )]
> > rK1: (String) => Unit =
>
> > scala> rK1( "Erp" )
> > rK1( "Erp" )
> > resuming...
> > searching kmap : Map()
>
> > scala>
>
> > What is the intended semantics? Should they capture the same environments?
> > Should the two executions be the same?
>
> > Best wishes in the New Year,
>
> > --greg
>
> > P.S. As a side note, i used to be able to serialize/deserialize delimited
> > continuations using XStream. This fails now with
>
> > scala> val rK1 = new XStream( new JettisonMappedXmlDriver() ).fromXML( ks1 )
> > val rK1 = new XStream( new JettisonMappedXmlDriver() ).fromXML( ks1 )
> > com.thoughtworks.xstream.converters.ConversionException: Element fun of
> > type scala.util.continuations.package$$anonfun$shiftR$1 is not defined as
> > field in type scala.util.continuations.ControlContext$$anonfun$map$2
> > ---- Debugging information ----
> > class               :
> > scala.util.continuations.ControlContext$$anonfun$map$2$$anonfun$apply$1
> > required-type       : scala.util.continuations.ControlContext$$anonfun$map$2
> > path                :
> > /scala.util.continuations.ControlContext$$anonfun$map$2$$anonfun$apply$1/$o uter[2]/fun
> > line number         : -1
> > -------------------------------
> > at
> > com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. writeValueToImplicitCollection(AbstractReflectionConverter.java:283)
> > at
> > com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. doUnmarshal(AbstractReflectionConverter.java:236)
> > at
> > com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. unmarshal(AbstractReflectionConverter.java:162)
> > at
> > com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.jav a:82)
> > at
> > com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(Abstrac tReferenceUnmarshaller.java:63)
> > at
> > com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshal ler.java:76)
> > at
> > com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. unmarshallField(AbstractReflectionConverter.java:246)
> > at
> > com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. doUnmarshal(AbstractReflectionConverter.java:218)
> > at
> > com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. unmarshal(AbstractReflectionConverter.java:162)
> > at
> > com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.jav a:82)
> > at
> > com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(Abstrac tReferenceUnmarshaller.java:63)
> > at
> > com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshal ler.java:76)
> > at
> > com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshal ler.java:60)
> > at
> > com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java: 137)
> > at
> > com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(Abs tractTreeMarshallingStrategy.java:33)
> > at
>
> ...
>
> read more »

Meredith Gregory
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Continuation behavior
Dear James,
Thanks for your note! Indeed, if we reorder the code as in the link here, we see the following trace. i was confused about the interaction of the closure capture and the mutability. i was assuming the closure would capture the map and not a copy, so that the subsequent mutation would show up in the reconstituted closure.
Best wishes,
--greg
scala> :load /Users/lgm/work/src/projex/stellar/mdp4tw/SpecialK.SDEC/scripts/k.scala calaLoading /Users/lgm/work/src/projex/stellar/mdp4tw/SpecialK.SDEC/scripts/k.scala...import scala.collection.mutable.HashMap import scala.util.continuations._import com.thoughtworks.xstream.XStreamimport com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver import biz.source_code.base64Coder.Base64Coderimport java.io.ObjectInputStreamimport java.io.ByteArrayInputStream import java.io.ObjectOutputStreamimport java.io.ByteArrayOutputStreamdefined module ObjectToString defined module OK
scala> OK.coral( false )( kmap1, ksmap1, "Erp" ) OK.coral( false )( kmap1, ksmap1, "Erp" )continuation as string: rO0ABXNyAEdzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHQkJGFub25mdW4kbWFwJDIkJGFub25mdW4kYXBwbHkkMQAAAAAAAAAAAgADTAAGJG91dGVydAA4THNjYWxhL3V0aWwvY29udGludWF0aW9ucy9Db250cm9sQ29udGV4dCQkYW5vbmZ1biRtYXAkMjtMAANrJDF0ABFMc2NhbGEvRnVuY3Rpb24xO0wABXRociQxcQB+AAJ4cHNyADZzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHQkJGFub25mdW4kbWF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gABTAAGJG91dGVycQB+AA54cHEAfgATc3IAMXNjYWxhLnV0aWwuY29udGludWF0aW9ucy5wYWNrYWdlJCRhbm9uZnVuJHJlc2V0JDEAAAAAAAAAAAIAAHhwc3IAOnNjYWxhLnV0aWwuY29udGludWF0aW9ucy5Db250cm9sQ29udGV4dCQkYW5vbmZ1biRmb3JlYWNoJDEAAAAAAAAAAAIAAHhw resuming...searching kmap : Map(Erp -> <function1>)fns is <function1>
scala> kmap1( "Erp" )( "Erp" )kmap1( "Erp" )( "Erp" ) resuming...searching kmap : Map(Erp -> <function1>)fns is <function1>
scala> val rK1 = ObjectToString.fromString( ksmap1( "Erp" ) ).asInstanceOf[( String => Unit )] ing => Unit )]rK1: (String) => Unit = <function1>
scala> rK1( "Erp" )rK1( "Erp" )resuming... searching kmap : Map(Erp -> <function1>)fns is <function1>
scala> 
On Fri, Dec 30, 2011 at 10:54 AM, James Douglas <jamesdouglas [at] gmail [dot] com> wrote:
Please pardon the mangled text.  I put the same source and REPL output
here: https://gist.github.com/1541001

On Dec 30, 10:49 am, James Douglas <jamesdoug [dot] [dot] [dot] [at] gmail [dot] com> wrote:
> > ksmap += ( ( key, ks ) )
> > kmap += ( ( key, k ) )
>
> It looks like when you add ks to ksmap it represents a closure around
> k, so the subsequent addition to kmap is not included.  If you add a
> line to the continuation to print out the object reference id for
> kmap, you'll see it is different for the two environments:
>
>       println( "resuming..." )
>       println( "searching kmap : " + kmap )
>       println( "kmap id : " + System.identityHashCode(kmap)) // add
> this line
>
> scala> val ksmap1 = new HashMap[String,String]( )
> ksmap1: scala.collection.mutable.HashMap[String,String] = Map()
>
> scala> val kmap1 = new HashMap[String,(String => Unit)]( )
> kmap1: scala.collection.mutable.HashMap[String,String => Unit] = Map()
>
> scala> OK.coral( false )( kmap1, ksmap1, "Erp" )
> continuation as string:
> rO0ABXNyAEdzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHQkJGFub25mdW4 kbWFwJDIkJGFub25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pdyQkaXckJGl3JCRpdyQk aXckJGl3JCRpdyQkaXckJGl3JCRpdyQkaXckJGl3JCRpdyQkaXckJGl3JCRpdyQkaXckJGl3JCR pdyQkaXckJGl3JCRpdyQkaXckJGl3JCRpdyRPSyQkYW5vbmZ1biRjb3JhbCQxAAAAAAAAAAACAA RaAAx1c2VYU3RyZWFtJDFMAAVrZXkkMXQAEkxqYXZhL2xhbmcvU3RyaW5nO0wABmttYXAkMXQAI kxzY2FsYS9jb2xsZWN0aW9uL211dGFibGUvSGFzaE1hcDtMAAdrc21hcCQxcQB
> +ABJ4cAB0AANFcnBzcgAgc2NhbGEuY29sbGVjdGlvbi5tdXRhYmxlLkhhc2hNYXAAAAAAAAAAAQ MAAHhwdwkAAALuAAAAAAB4c3EAfgAVdwkAAALuAAAAAAB4cHNyAJskbGluZTE3My4kcmVhZCQka XckJGl3JCRpdyQkaXckJGl3JCRpdyQkaXckJGl3JCRpdyQkaXckJGl3JCRpdyQkaXckJGl3JCRp dyQkaXckJGl3JCRpdyQkaXckJGl3JCRpdyQkaXckJGl3JCRpdyQkaXckJGl3JE9LJCRhbm9uZnV uJGNvcmFsJDEkJGFub25mdW4kYXBwbHkkMgAAAAAAAAAAAgABTAAGJG91dGVycQB
> +AA54cHEAfgATc3IAMXNjYWxhLnV0aWwuY29udGludWF0aW9ucy5wYWNrYWdlJCRhbm9uZnVuJH Jlc2V0JDEAAAAAAAAAAAIAAHhwc3IAOnNjYWxhLnV0aWwuY29udGludWF0aW9ucy5Db250cm9sQ 29udGV4dCQkYW5vbmZ1biRmb3JlYWNoJDEAAAAAAAAAAAIAAHhw
> resuming...
> searching kmap : Map(Erp -> <function1>)
> kmap id : 24530783
> fns is <function1>
>
> scala> kmap1( "Erp" )( "Erp" )
> resuming...
> searching kmap : Map(Erp -> <function1>)
> kmap id : 24530783
> fns is <function1>
>
> scala> val rK1 = ObjectToString.fromString( ksmap1( "Erp" )
>      | ).asInstanceOf[( String => Unit )]
> rK1: String => Unit = <function1>
>
> scala> rK1( "Erp" )
> resuming...
> searching kmap : Map()
> kmap id : 4430367
>
> On Dec 29, 4:29 pm, Meredith Gregory <lgreg [dot] mered [dot] [dot] [dot] [at] gmail [dot] com> wrote:
>
>
>
>
>
>
>
> > Dear Continually Scala,
>
> > Here's a little snippet of code <http://paste.pocoo.org/show/527498/> that
> > captures continuations
>
> >    - as an in-memory representation, and
> >    - as a serialized representation
>
> > The two different captures give different behaviors because they capture
> > different environments. Here's a trace to illustrate:
>
> > Welcome to Scala version 2.9.0.1 (Java HotSpot(TM) 64-Bit Server VM, Java
> > 1.6.0_29).
> > Type in expressions to have them evaluated.
> > Type :help for more information.
>
> > scala> :load
> > /Users/lgm/work/src/projex/stellar/mdp4tw/SpecialK.SDEC/scripts/k.scala
> > cala
> > Loading
> > /Users/lgm/work/src/projex/stellar/mdp4tw/SpecialK.SDEC/scripts/k.scala...
> > import scala.collection.mutable.HashMap
> > import scala.util.continuations._
> > import com.thoughtworks.xstream.XStream
> > import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver
> > import biz.source_code.base64Coder.Base64Coder
> > import java.io.ObjectInputStream
> > import java.io.ByteArrayInputStream
> > import java.io.ObjectOutputStream
> > import java.io.ByteArrayOutputStream
> > defined module ObjectToString
> > defined module OK
>
> > scala> val ksmap1 = new HashMap[String,String]( )
> > val ksmap1 = new HashMap[String,String]( )
> > ksmap1: scala.collection.mutable.HashMap[String,String] = Map()
>
> > scala> val kmap1 = new HashMap[String,(String => Unit)]( )
> > val kmap1 = new HashMap[String,(String => Unit)]( )
> > kmap1: scala.collection.mutable.HashMap[String,(String) => Unit] = Map()
>
> > scala> OK.coral( false )( kmap1, ksmap1, "Erp" )
> > OK.coral( false )( kmap1, ksmap1, "Erp" )
> > continuation as string:
> > rO0ABXNyAEdzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29udHJvbENvbnRleHQkJGFub25mdW4 kbWFwJDIkJGFub25mdW4kYXBwbHkkMQAAAAAAAAAAAgADTAA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AAAAB4c3EAfgAVdwkAAALuAAAAAAB4cHNyAEIkbGluZTExLiRyZWFkJCRpdyQkaXckJGl3JCR pdyRPSyQkYW5vbmZ1biRjb3JhbCQxJCRhbm9uZnVuJGFwcGx5JDIAAAAAAAAAAAIAAUwABiRvdX RlcnEAfgAOeHBxAH4AE3NyADFzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMucGFja2FnZSQkYW5vb mZ1biRyZXNldCQxAAAAAAAAAAACAAB4cHNyADpzY2FsYS51dGlsLmNvbnRpbnVhdGlvbnMuQ29u dHJvbENvbnRleHQkJGFub25mdW4kZm9yZWFjaCQxAAAAAAAAAAACAAB4cA==
> > resuming...
> > searching kmap : Map(Erp -> <function1>)
> > fns is <function1>
>
> > scala> kmap1( "Erp" )( "Erp" )
> > kmap1( "Erp" )( "Erp" )
> > resuming...
> > searching kmap : Map(Erp -> <function1>)
> > fns is <function1>
>
> > scala> val rK1 = ObjectToString.fromString( ksmap1( "Erp" )
> > ).asInstanceOf[( String => Unit )]
> > ing => Unit )]
> > rK1: (String) => Unit = <function1>
>
> > scala> rK1( "Erp" )
> > rK1( "Erp" )
> > resuming...
> > searching kmap : Map()
>
> > scala>
>
> > What is the intended semantics? Should they capture the same environments?
> > Should the two executions be the same?
>
> > Best wishes in the New Year,
>
> > --greg
>
> > P.S. As a side note, i used to be able to serialize/deserialize delimited
> > continuations using XStream. This fails now with
>
> > scala> val rK1 = new XStream( new JettisonMappedXmlDriver() ).fromXML( ks1 )
> > val rK1 = new XStream( new JettisonMappedXmlDriver() ).fromXML( ks1 )
> > com.thoughtworks.xstream.converters.ConversionException: Element fun of
> > type scala.util.continuations.package$$anonfun$shiftR$1 is not defined as
> > field in type scala.util.continuations.ControlContext$$anonfun$map$2
> > ---- Debugging information ----
> > class               :
> > scala.util.continuations.ControlContext$$anonfun$map$2$$anonfun$apply$1
> > required-type       : scala.util.continuations.ControlContext$$anonfun$map$2
> > path                :
> > /scala.util.continuations.ControlContext$$anonfun$map$2$$anonfun$apply$1/$o uter[2]/fun
> > line number         : -1
> > -------------------------------
> > at
> > com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. writeValueToImplicitCollection(AbstractReflectionConverter.java:283)
> > at
> > com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. doUnmarshal(AbstractReflectionConverter.java:236)
> > at
> > com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. unmarshal(AbstractReflectionConverter.java:162)
> > at
> > com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.jav a:82)
> > at
> > com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(Abstrac tReferenceUnmarshaller.java:63)
> > at
> > com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshal ler.java:76)
> > at
> > com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. unmarshallField(AbstractReflectionConverter.java:246)
> > at
> > com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. doUnmarshal(AbstractReflectionConverter.java:218)
> > at
> > com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter. unmarshal(AbstractReflectionConverter.java:162)
> > at
> > com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.jav a:82)
> > at
> > com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(Abstrac tReferenceUnmarshaller.java:63)
> > at
> > com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshal ler.java:76)
> > at
> > com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshal ler.java:60)
> > at
> > com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java: 137)
> > at
> > com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(Abs tractTreeMarshallingStrategy.java:33)
> > at
>
> ...
>
> read more »



--
L.G. Meredith
Managing Partner
Biosimilarity LLC
7329 39th Ave SWSeattle, WA 98136

+1 206.650.3740

http://biosimilarity.blogspot.com

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland