Scala DSL 仅适用于括号
Scala DSL works only with parenthesis
我有以下代码
type RequestWithParams[T] = (ApiRequest[T], Map[String, String])
implicit class RequestWithParamsWrapper[T](request: ApiRequest[T]) {
def ? (params: Map[String, String]) : RequestWithParams[T] = (request, params)
}
type RequestWithBody[T] = (ApiPostRequest[T], ApiModel)
implicit class RequestWithBodyWrapper[T](request: ApiPostRequest[T]) {
def < (body: ApiModel) : RequestWithBody[T] = (request, body)
}
我能做到
val response = getReleasesUsingGET ? Map[String, String]("a" -> "b")
val response2 = createReleaseUsingPOST < ReleasePostRequest(Some("artifactId"), Some("groupId"), None, Some("1.0.1"))
现在我想结合params和body。这是我的代码
type RequestWithParamsAndBody[T] = (RequestWithBody[T], Map[String, String])
implicit class RequestWithParamsAndBodyWrapper[T: TypeTag](request: RequestWithBody[T]) {
def ?(params: Map[String, String]) : RequestWithParamsAndBody[T] = (request, params)
}
如果我写这个作品
val response3 = (createReleaseUsingPOST < ReleasePostRequest(Some("artifactId"), Some("groupId"), None, Some("1.0.1"))) ? Map[String, String]("a" -> "b")
但是如果我写这个就不行
val response3 = createReleaseUsingPOST < ReleasePostRequest(Some("artifactId"), Some("groupId"), None, Some("1.0.1")) ? Map[String, String]("a" -> "b")
Error value ? is not a member of ReleasePostRequest
有没有办法不用括号使用DSL?提前致谢
如 here 所述,?
的优先级高于 <
。 ?
属于 "all other special characters" 的类别。所以你唯一能做的就是要么使用括号,要么使用其他运算符。例如 |?
的优先级低于 <
,所以
createReleaseUsingPOST < ReleasePostRequest(Some("artifactId"), Some("groupId"), None, Some("1.0.1")) |? Map[String, String]("a" -> "b")
会按照你想要的方式解决。
我有以下代码
type RequestWithParams[T] = (ApiRequest[T], Map[String, String])
implicit class RequestWithParamsWrapper[T](request: ApiRequest[T]) {
def ? (params: Map[String, String]) : RequestWithParams[T] = (request, params)
}
type RequestWithBody[T] = (ApiPostRequest[T], ApiModel)
implicit class RequestWithBodyWrapper[T](request: ApiPostRequest[T]) {
def < (body: ApiModel) : RequestWithBody[T] = (request, body)
}
我能做到
val response = getReleasesUsingGET ? Map[String, String]("a" -> "b")
val response2 = createReleaseUsingPOST < ReleasePostRequest(Some("artifactId"), Some("groupId"), None, Some("1.0.1"))
现在我想结合params和body。这是我的代码
type RequestWithParamsAndBody[T] = (RequestWithBody[T], Map[String, String])
implicit class RequestWithParamsAndBodyWrapper[T: TypeTag](request: RequestWithBody[T]) {
def ?(params: Map[String, String]) : RequestWithParamsAndBody[T] = (request, params)
}
如果我写这个作品
val response3 = (createReleaseUsingPOST < ReleasePostRequest(Some("artifactId"), Some("groupId"), None, Some("1.0.1"))) ? Map[String, String]("a" -> "b")
但是如果我写这个就不行
val response3 = createReleaseUsingPOST < ReleasePostRequest(Some("artifactId"), Some("groupId"), None, Some("1.0.1")) ? Map[String, String]("a" -> "b")
Error value ? is not a member of ReleasePostRequest
有没有办法不用括号使用DSL?提前致谢
如 here 所述,?
的优先级高于 <
。 ?
属于 "all other special characters" 的类别。所以你唯一能做的就是要么使用括号,要么使用其他运算符。例如 |?
的优先级低于 <
,所以
createReleaseUsingPOST < ReleasePostRequest(Some("artifactId"), Some("groupId"), None, Some("1.0.1")) |? Map[String, String]("a" -> "b")
会按照你想要的方式解决。