Last active
June 24, 2017 08:19
-
-
Save deffence1776/8193d2f4db73cbfc7f0cfe9e130ce443 to your computer and use it in GitHub Desktop.
kotlin+SparkJavaでメソッド参照利用して、コントローラのインスタンスメソッドを渡す。
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
/** | |
* 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" | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1.1からできるようになった