Skip to content

Instantly share code, notes, and snippets.

@liancheng
Created December 18, 2014 09:49
Show Gist options
  • Save liancheng/1441a1806569fce38359 to your computer and use it in GitHub Desktop.
Save liancheng/1441a1806569fce38359 to your computer and use it in GitHub Desktop.
Scala function serialization
import java.io._
object Main {
def main(args: Array[String]): Unit = {
val stream = new ByteArrayOutputStream()
val out = new ObjectOutputStream(stream)
def foo(): String => String = {
val test = "hello"
def bar(name: String): String = s"$test $name"
bar
}
out.writeObject(foo())
out.close()
val bytes = stream.toByteArray
val in = new ObjectInputStream(new ByteArrayInputStream(bytes))
val fn: String => String = in.readObject().asInstanceOf[String => String]
println(fn("world"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment