Skip to content

Instantly share code, notes, and snippets.

@deffence1776
Last active June 24, 2017 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deffence1776/8193d2f4db73cbfc7f0cfe9e130ce443 to your computer and use it in GitHub Desktop.
Save deffence1776/8193d2f4db73cbfc7f0cfe9e130ce443 to your computer and use it in GitHub Desktop.
kotlin+SparkJavaでメソッド参照利用して、コントローラのインスタンスメソッドを渡す。
/**
* Created by matsushitamasatsugu on 2016/05/04.
*/
import spark.Request
import spark.Response
import spark.Spark.*
fun main(args: Array<String>) {
val c =Con("state")
get("/hello",c.handlerFun(Con::hello))
//まあ、これでもいいきがするが。。。get("/hello",{req:Request, res:Response->c.hello(req,res)})
}
//参考 :http://taro.hatenablog.jp/entry/2015/02/24/215413
// kotlinのメソッド参照はレシーバの型までチェックしているからJava8のようにインスタンスメソッドそのまま渡せない。
//仕様としてはkotlinの方が好きではある。
fun <H> H.handlerFun(handle: H.(req:Request, res:Response)->Any) :(Request, Response)->Any
={req:Request, res:Response ->this.handle(req,res)}
/**
* コントローラ
*/
class Con(val msg :String){
fun hello(request:Request, respons: Response ):String{
return "Hello, world! $msg"
}
}
@deffence1776
Copy link
Author

1.1からできるようになった

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment