Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
object ResponseDemo:
import javax.ws.rs.core.Response
trait HttpResponse[A]:
extension (a: A) def response: Response
object HttpResponse:
given stringResponse : HttpResponse[String] with
extension (str: String)
def response: Response = Response.status(Response.Status.OK).entity(str).build
given optHttpResponse[T : HttpResponse]: HttpResponse[Option[T]] with
extension (opt: Option[T])
def response: Response =
opt match
case None => Response.status(404).build()
case Some(t) => summon[HttpResponse[T]].response(t)
end ResponseDemo
object Test2 extends App:
import ResponseDemo.HttpResponse.stringResponse
println("all is good".response)
val someValue: Option[String] = Some("all is good")
import ResponseDemo.HttpResponse.optHttpResponse
println(someValue.response)
val noValue: Option[String] = None
println(noValue.response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment