Roiocam commented on PR #1304:
URL: https://github.com/apache/pekko/pull/1304#issuecomment-2106689353
Use the cacheable partial function, and then verify it works.
```scala
var calTime: Int = 0
def entityId(value: Any): String = {
calTime = calTime + 1
value match {
case "wrong" => null
case _ => String.valueOf(value)
}
}
def extractEntityIdFromExtractor: PartialFunction[Any, Any] =
new scala.runtime.AbstractPartialFunction[Any, (String, Any)] {
var cache: String = _
override def isDefinedAt(msg: Any): Boolean = {
cache = entityId(msg)
cache != null
}
override def apply(x: Any): (String, Any) = (cache, x)
}
lazy val xx = extractEntityIdFromExtractor
lazy val yy: PartialFunction[Any, Any] = {
case message if entityId(message) != null => (entityId(message), message)
}
assert(!xx.isDefinedAt("wrong"))
assert(!yy.isDefinedAt("wrong"))
calTime = 0
val health = "health"
println(xx.isDefinedAt(health))
println(xx(health))
println(s"caltime = $calTime")
println("=====")
calTime = 0
println(yy.isDefinedAt(health))
println(yy(health))
println(s"caltime = $calTime")
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]