Created
December 18, 2014 09:49
-
-
Save liancheng/1441a1806569fce38359 to your computer and use it in GitHub Desktop.
Scala function serialization
This file contains 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._ | |
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