[英]Defining new TYPE for partial function with an implicit parameters
我正在尝试创建一个具有部分函数模式的新TYPE。 例:
新类型 :
type RouteFunc = (String, HttpServletRequest, HttpServletResponse) => Unit
用法 :
def myFunc(str:字符串,请求:HttpServletRequest,响应HttpServletResponse)myFunc(“”,Any,Any)
对于显式传递的参数,这确实很好用,但是我想更改此类型定义,以便将HttpServletRequest, HttpServletResponse
隐式与显式传递。
预期结果 :
def myFunc(str: String)(implicit request: HttpServletRequest, response HttpServletResponse)
我找不到一种方法来更改TYPE结构/定义以适应预期的模式,这是可能的还是语言的限制?
编辑 :
用法:
object Routes{
type RouteFunc = (String, HttpServletRequest, HttpServletResponse) => Unit
val routes = Map[String, RouteFunc](
"/data" -> DataSets.dashboard
)
def evaluateRoute()(implicit request: HttpServletRequest, response: HttpServletResponse) = {
val path = request.getPathInfo
val route = routes(path)
route(path, request, response)
}
}
object DataSets{
def dashboard(path: String, request: HttpServletRequest, response: HttpServletResponse) = {
response.setContentType("text/html")
response.setCharacterEncoding("UTF-8")
response.getWriter.write("Hello World")
}
}
我希望def dashboard
看起来像:
def dashboard(path: String)(implicit request: HttpServletRequest, response: HttpServletResponse)
编辑2 :
最后,我明确地传递了参数,因为对于@SergGr和@slouc所解释的当前版本的Scala,这是不可能的:
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import controllers.data._
import Routes._
class RoutingController() extends JettyHttpServlet {
type RouteFunc = (String, HttpServletRequest, HttpServletResponse) => Unit
type HttpRequest = String
type Path = String
val routes = Map[(String, HttpRequest), RouteFunc](
(GET, "/data") -> Data.dashboard,
(GET, "/assets/*") -> Assets.getAsset
)
override def Get()(implicit request: HttpServletRequest, response: HttpServletResponse): Unit = {
val path = request.getPathInfo
val pathTerms = path.split("/")
val getPaths = routes.filter(_._1._1 == request.getMethod.toUpperCase)
val filteredList = getPaths.flatMap{
route =>
if(route._1._2 == path){
Option(route)
}else {
val s = route._1._2.split("/")
if(route._1._2.startsWith("/assets")){
Option(route)
}else
None
}
}.toSeq
filteredList.head._2(path, request, response)
}
}
----------------------
import jetty.Routes
import jetty.Responses._
object Data {
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
def dashboard(path: String, eq: HttpServletRequest, qw: HttpServletResponse): Unit = {
implicit val s = eq
implicit val r = qw
Ok(Routes.html, views.html.data.dashboard.render("Dashboard").toString())
}
}
如果我的问题正确无误,您想使用隐式参数定义一个函数。 这是不可能的。 它不仅是一种语言约束,而且还是一种逻辑约束。 函数是一个数学概念,那里没有所谓的“隐式参数”。 无法提供参数的子集,而其余部分则“从作用域”中获取。
在Scala中,有一种方法可以将方法转换为函数。 有问题的机制称为eta扩展。 给定一些方法fooMethod,则可以将对应的函数fooFunction定义为
def fooMethod(i: Input): Output
val fooFunction: Input => Output = (i: Input) => fooMethod(i)
// or
def fooMethod(i1: Input1, i2: Input2): Output
val fooFunction: Input1 => Input2 => Output = (i1: Input1) => (i: Input2) => fooMethod(i1, i2)
// or
def fooMethod(): Output
val fooFunction: Unit => Output = () => fooMethod()
如果您提供了需要函数的方法,则编译器将自动执行eta扩展。 如果无法自动执行,则可以手动执行:
val fooFunction = fooMethod _
但是,由于前面提到的原因,如果您引入隐式参数,即使该技巧也失败了。
例:
trait HttpServletRequest
trait HttpServletResponse
type RouteFunc = String => (HttpServletRequest, HttpServletResponse) => Unit
implicit val request = new HttpServletRequest {}
implicit val response = new HttpServletResponse {}
def myMethod1(str: String)(request: HttpServletRequest, response: HttpServletResponse) = println("1")
def myMethod2(str: String)(implicit request: HttpServletRequest, response: HttpServletResponse) = println("2")
val myFunc1: RouteFunc = myMethod1 _ // compiles fine
val myFunc2: RouteFunc = myMethod2 _ // nope
编辑:
鉴于您的编辑,这就是我的做法:
trait HttpServletRequest
trait HttpServletResponse
object Routes {
implicit val request = new HttpServletRequest {}
implicit val response = new HttpServletResponse {}
type RouteFunc = String => Unit
val routes = Map[String, RouteFunc]("/data" -> DataSets.dashboard())
}
object DataSets {
def dashboard()(implicit request: HttpServletRequest, response: HttpServletResponse): RouteFunc =
(path: String) => {
// impl of dashboard, uses request and response
}
}
您将路由定义为功能String => Unit
,并将仪表板定义为采用隐式请求和响应并为您构造路由的方法。
编辑2:
似乎在将来的Scala版本之一中将提供对带有隐式参数的函数的支持。 我不喜欢这样,我不会使用它,即使没有它,我们在Scala中也会有太多隐含的地狱,但是很高兴知道这一点,这样我就不会一直声称这是不可能的:)
从Scala 2.12开始不支持隐式函数。 看起来当Dotty成为Scala的官方编译器时,他们计划将其用于更高版本中的一个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.