Scala DSL仅适用于括号

时间:2017-03-01 10:44:20

标签: scala dsl implicit

我有以下代码

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?提前致谢

1 个答案:

答案 0 :(得分:1)

正如here所述,?的优先级高于<?属于&#34;所有其他特殊字符&#34;的类别。所以你唯一能做的就是使用括号,或者使用其他运算符。例如,|?的优先级低于<,因此

createReleaseUsingPOST < ReleasePostRequest(Some("artifactId"), Some("groupId"), None, Some("1.0.1")) |? Map[String, String]("a" -> "b")

会以你想要的方式运作。