Ruben J Garcia
Ruben J Garcia

Reputation: 344

Scala DSL works only with parenthesis

I have the following code

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)
}

I can do this

val response = getReleasesUsingGET ? Map[String, String]("a" -> "b")
val response2 = createReleaseUsingPOST < ReleasePostRequest(Some("artifactId"), Some("groupId"), None, Some("1.0.1"))

Now I want to combine params and body. This is my code

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)
}

If I write this works

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

But if I write this it doesn't work

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

Is there a way to use the DSL without parenthesis? Thanks in advance

Upvotes: 1

Views: 52

Answers (1)

Jasper-M
Jasper-M

Reputation: 15086

As explained here, ? has higher precedence than <. ? belongs to the category of "all other special characters". So the only thing you can do is either use parentheses, or use other operators. For instance |? has lower precedence than <, so

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

would work out the way you want.

Upvotes: 1

Related Questions