alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (mock.scala)

This example Scala source code file (mock.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

anyref, array, callable, closeable, closeable, hi, hi, io, runnable, t, test, test, threading, threads, unit, universalfn, universalfn

The Scala mock.scala source code

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)
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala mock.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.