summaryrefslogtreecommitdiff
path: root/test/files/run/mock.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-11-28 02:38:38 +0000
committerPaul Phillips <paulp@improving.org>2010-11-28 02:38:38 +0000
commit16b3e8c1d70a0ddf5307c6564fd34a4854e4bf77 (patch)
treedb6782e00e0aed4ee3085e14740b0c4d9df49e7d /test/files/run/mock.scala
parenta5553b8384a6e4cb6071846368bb0d17be2e246f (diff)
downloadscala-16b3e8c1d70a0ddf5307c6564fd34a4854e4bf77.tar.gz
scala-16b3e8c1d70a0ddf5307c6564fd34a4854e4bf77.tar.bz2
scala-16b3e8c1d70a0ddf5307c6564fd34a4854e4bf77.zip
Fleshed out the mock code a little further so i...
Fleshed out the mock code a little further so it's easy for closures to become SAMs. // implicit not necessary, but improves fun factor scala> implicit def mkUFn(x: AnyRef) = scala.tools.reflect.UniversalFn(x) mkUFn: (x: AnyRef)scala.tools.reflect.UniversalFn scala> (() => 5*5*5).as[java.util.concurrent.Callable[Int]] res1: java.util.concurrent.Callable[Int] = <function1> scala> res1.call res2: Int = 125 No review.
Diffstat (limited to 'test/files/run/mock.scala')
-rw-r--r--test/files/run/mock.scala29
1 files changed, 29 insertions, 0 deletions
diff --git a/test/files/run/mock.scala b/test/files/run/mock.scala
new file mode 100644
index 0000000000..e414e51e2d
--- /dev/null
+++ b/test/files/run/mock.scala
@@ -0,0 +1,29 @@
+import scala.tools.reflect._
+import java.util.concurrent.Callable
+import java.io.Closeable
+
+object Test {
+ // It'd be really nice about now if functions had a common parent.
+ implicit def interfaceify(x: AnyRef): UniversalFn = UniversalFn(x)
+
+ def runner(x: Runnable) = x.run()
+ def caller[T](x: Callable[T]): T = x.call()
+ def closer(x: Closeable) = x.close()
+
+ def main(args: Array[String]): Unit = {
+ var counter = 0
+ val closure = () => {
+ counter += 1
+ println("Hi, thanks for calling: that makes " + counter + " times.")
+ counter
+ }
+
+ val int1 = closure.as[Runnable]
+ val int2 = closure.as[Callable[Int]]
+ val int3 = closure.as[Closeable]
+
+ runner(int1)
+ caller(int2)
+ closer(int3)
+ }
+}