[TODO]
-
-
Save hubte1g/cb56e4f4ae656643d6a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ObjectOutputStream | |
import java.io.ObjectInputStream | |
import java.io.FileOutputStream | |
import java.io.FileInputStream | |
class OuterScopeThatDefinesFunction extends Serializable { | |
// This method returns a function whose $outer scope is this class. | |
// Note that the function does not reference any variables in this scope, | |
// so the generated class won't contain any fields. | |
def func = (a: Int) => a | |
} | |
// This class is a simple wrapper that holds a function: | |
class UsesFunction(val func: Int => Int) extends Serializable { } | |
object Main { | |
def main(args: Array[String]) { | |
if (args.size == 0) { | |
println("Serializing object to test.bin") | |
// Define a function in an outer scope | |
val functionDefinitionScope = new OuterScopeThatDefinesFunction() | |
// Put that function into an object | |
val c = new UsesFunction(functionDefinitionScope.func) | |
// Serialize that object | |
val fs = new FileOutputStream("test.bin") | |
val os = new ObjectOutputStream(fs) | |
os.writeObject(c) | |
os.close() | |
fs.close() | |
} | |
else { | |
println("Deserializing object from test.bin") | |
// Read the object serialized in the previous step | |
val fs = new FileInputStream("test.bin") | |
val os = new ObjectInputStream(fs) | |
val c = os.readObject().asInstanceOf[UsesFunction] | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
if [ -z $1 ]; then | |
echo "Usage: $0 scalaVersion" | |
exit -1 | |
fi | |
SCALA_VERSION=$1 | |
echo "Testing with Scala $1" | |
set -x | |
rm test.bin | |
sbt "++ $SCALA_VERSION" clean compile | |
scala -cp target/scala-*/classes/ Main | |
scala -cp target/scala-*/classes/ Main read | |
rm target/scala-*/classes/OuterScopeThatDefinesFunction.class | |
scala -cp target/scala-*/classes/ Main read |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment