Cannot resolve overloaded method 'startTimerWithFixedDelay' - scala

I do not understand, why I get an overloaded error inside "timers.startTimerWithFixedDelay". I added 3 parameter to ensure to take the right method, but it appears that he also finds the method with just 2 parameters highly attractive.
package main
import akka.actor.typed.{ActorSystem, Behavior}
import akka.actor.typed.receptionist.Receptionist
import akka.actor.typed.scaladsl.Behaviors
import scala.concurrent.duration.{Duration, FiniteDuration, MINUTES}
object Guardian {
case object Tick
val start: Behavior[Nothing] =
Behaviors.setup[Receptionist.Listing] { context =>
Behaviors.withTimers { timers =>
timers.startTimerWithFixedDelay(Tick, Tick, FiniteDuration(Duration("3 seconds").toSeconds, MINUTES))
Behaviors.same
}
}.narrow
}
object Application extends App {
ActorSystem[Nothing](Guardian.start, "system")
}
Inside TimerScheduler.scala does it look like, that he can't he decide between:
def startTimerWithFixedDelay(msg: T, delay: FiniteDuration): Unit
def startTimerWithFixedDelay(key: Any, msg: T, delay: FiniteDuration): Unit
Why is he not taking the one with 3 parameter?
Compiler Error message:
[error] ... overloaded method startTimerWithFixedDelay with alternatives:
[error] (msg: akka.actor.typed.receptionist.Receptionist.Listing,delay: scala.concurrent.duration.FiniteDuration)Unit <and>
[error] (key: Any,msg: akka.actor.typed.receptionist.Receptionist.Listing,delay: scala.concurrent.duration.FiniteDuration)Unit

Tick is not a Receptionist.Listing so neither version matches.

Related

Recursively wrapping method invocations with compiler plugins/macros

OUTLINE
I have an API that looks something like this:
package com.example
object ExternalApi {
def create[T <: SpecialElement](elem: T): TypeConstructor[T] =
TypeConstructor(elem)
def create1[T <: SpecialElement](elem: T): TypeConstructor[T] =
TypeConstructor(elem)
def create2[T <: SpecialElement](elem: T): TypeConstructor[T] =
TypeConstructor(elem)
//...
}
object MyApi {
def process[T <: TypeConstructor[_ <: SpecialElement]](
l: T,
metadata: List[String]): T = {
println("I've been called!")
//do some interesting stuff with the List's type parameter here
l
}
}
case class TypeConstructor[E](elem: E)
trait SpecialElement
The ExternalApi (which is actually external to my lib, so no modifying that) has a series of calls that I'd like to automatically wrap with MyApi.process calls, with the metadata argument derived from the final type of T.
To illustrate, the calls to be wrapped could have any form, including nested calls, and calls within other AST subtree types (such as Blocks), e.g. :
package com.example.test
import com.example.{ExternalApi, SpecialElement}
object ApiPluginTest extends App {
//one possible form
val targetList = ExternalApi.create(Blah("name"))
//and another
ExternalApi.create2(ExternalApi.create1(Blah("sth")).elem)
//and yet another
val t = {
val sub1 = ExternalApi.create(Blah("anything"))
val sub2 = ExternalApi.create1(sub1.elem)
sub2
}
}
case class Blah(name: String) extends SpecialElement
Since compiler plugins handle matching structures within ASTs recursively "for free", I've decided to go with them.
However, due to the fact that I need to match a specific type signature, the plugin follows the typer phase.
Here's the code of the PluginComponent:
package com.example.plugin
import com.example.{SpecialElement, TypeConstructor}
import scala.tools.nsc.Global
import scala.tools.nsc.plugins.PluginComponent
import scala.tools.nsc.transform.Transform
class WrapInApiCallComponent(val global: Global)
extends PluginComponent
with Transform {
protected def newTransformer(unit: global.CompilationUnit) =
WrapInApiCallTransformer
val runsAfter: List[String] = List("typer") //since we need the type
val phaseName: String = WrapInApiCallComponent.Name
import global._
object WrapInApiCallTransformer extends Transformer {
override def transform(tree: global.Tree) = {
val transformed = super.transform(tree)
transformed match {
case call # Apply(_, _) =>
if (call.tpe != null && call.tpe.finalResultType <:< typeOf[
TypeConstructor[_ <: SpecialElement]]) {
println(s"Found relevant call $call")
val typeArguments = call.tpe.typeArgs.map(_.toString).toList
val listSymbOf = symbolOf[List.type]
val wrappedFuncSecondArgument =
q"$listSymbOf.apply(..$typeArguments)"
val apiObjSymbol = symbolOf[com.example.MyApi.type]
val wrappedCall =
q"$apiObjSymbol.process[${call.tpe.finalResultType}]($call, $wrappedFuncSecondArgument)"
//explicit typing, otherwise later phases throw NPEs etc.
val ret = typer.typed(wrappedCall)
println(showRaw(ret))
println("----")
ret
} else {
call
}
case _ => transformed
}
}
}
}
object WrapInApiCallComponent {
val Name = "api_embed_component"
}
This seems to resolve identifiers, as well as types, correctly, outputting e.g.:
Apply(TypeApply(Select(TypeTree().setOriginal(Ident(com.example.MyApi)), TermName("process")), List(TypeTree())), List(Apply(TypeApply(Select(Select(Select(Ident(com), com.example), com.example.MyApi), TermName("create")), List(TypeTree())), List(Apply(Select(Ident(com.example.test.Blah), TermName("apply")), List(Literal(Constant("name")))))), Apply(TypeApply(Select(TypeTree().setOriginal(Ident(scala.collection.immutable.List)), TermName("apply")), List(TypeTree())), List(Literal(Constant("com.example.test.Blah"))))))
Unfortunately, I get an error during compilation starting with:
scala.reflect.internal.FatalError:
[error]
[error] Unexpected tree in genLoad: com.example.MyApi.type/class scala.reflect.internal.Trees$TypeTree at: RangePosition([projectpath]/testPluginAutoWrap/compiler_plugin_test/src/main/scala/com/example/test/ApiPluginTest.scala, 108, 112, 112)
[error] while compiling: [projectpath]/testPluginAutoWrap/compiler_plugin_test/src/main/scala/com/example/test/ApiPluginTest.scala
[error] during phase: jvm
[error] library version: version 2.12.4
[error] compiler version: version 2.12.4
[error] reconstructed args: -Xlog-implicits -classpath [classpath here]
[error]
[error] last tree to typer: TypeTree(class String)
[error] tree position: line 23 of [projectpath]/testPluginAutoWrap/compiler_plugin_test/src/main/scala/com/example/test/ApiPluginTest.scala
QUESTION
It looks I'm screwing something up with type definitions, but what is it?
Specifically:
How do I correctly wrap every ExternalApi.createX call with an MyApi.process call, constrained by the requirements provided above?
NOTES
Given the amount of boilerplate required, I've set up a complete example project. It's available here.
The answer does not have to define a compiler plugin. If you're able to cover all the relevant calls with a macro, that is fine as well.
Originally the wrapped call was to something like: def process[T <: TypeConstructor[_ <: SpecialElement] : TypeTag](l: T): T, the setup here is actually a workaround. So if you are able to generate a wrapped call of this type, i.e. one that includes a runtime TypeTag[T], that's fine as well.

How to build a wrapper around scalatest's "it" keyword?

I am attempting to build a wrapper around scalatest's it keyword. However, this solution does not seem to work as intended. Moreover, it does not even compile:
trait MyFunSpec extends FunSpec {
private val _it: FunSpec.this.ItWord = it
protected def it(specText: String, testTags: org.scalatest.Tag*)
// ...do something extra here...
(testFun: => Any /* Assertion */)(implicit pos: Position): Unit = {
_it(specText, testTags: _*)(testFun)(pos)
// ...do something extra here...
}
}
The error message I am getting after compiling this code is as follows:
[error] MyFunSpec.scala: ambiguous reference to overloaded definition,
[error] both method it in trait MyFunSpec of type (specText: String, testTags:
org.scalatest.Tag*)(testFun: => Any)(implicit pos:
org.scalactic.source.Position)Unit
[error] and value it in trait FunSpecLike of type => FunSpecSpec.this.ItWord
[error] match argument types (String)
Please note the main idea is that method's name remains it, so renaming it to something like alternativeIt is not a satisfactory solution here.
Any suggestions, what am I doing wrong here? Any solution would be highly appreciated! Thanks!
Try this:
trait MyFunSpec extends FunSpec {
override protected val it = new ItWord {
override def apply(specText: String,testTags: org.scalatest.Tag*)(testFun: => Any)(implicit pos: org.scalactic.source.Position): Unit = {
println("Before")
super.apply(specText, testTags:_*)(testFun)(pos)
println("After")
}
}
}

Understanding implicit conversions

I'm reading Scala documentation of implicit conversions and decided to try it out:
object Main extends App {
val test = TestConversion(100)
test.its
}
class Test[T](val t : T) {
def its = println(t)
}
object Test {
def apply[T](t: T): Test[T] = new Test(t)
}
class TestConversion(v : Int) {
val value = v
}
object TestConversion{
implicit def implicitConversionTest2Int(ict : TestConversion): Test[Int] = Test(ict.value)
def apply(v : Int) = new TestConversion(v)
}
As it's said:
To define your own implicit conversions, you must first import
scala.language.implicitConversions (or invoke the compiler with
-language:implicitConversions). The feature must be explicitly enabled because it has pitfalls if used indiscriminately.
I tried it both in IntelliJ and online IdeOne and I didn't add anything special to make it compile.
What pitfalls it brings and why does it work without any imports?
You don't need to import anything.
The idea is that you can declare implicit conversion function wherever you want in the scope.
For example:
case class Coins(amount:Int)
case class Bills(amount:Int)
object Main {
implicit def billsToCoins(bills:Bills):Coins = Coins(bills.amount * 100)
def printCoins(coins:Coins) = println(s"You have ${coins.amount} coins." )
def main(args:Array[String]): Unit ={
printCoins(Bills(3))
}
}
I have declared here implicit function billsToCoins so it is available in scope of the main function. The only thing needed for the function to act as implicit converter is to have the implicit modifier, compiler will find it and use. You see that the printCoins function takes argument of the Coins type but I was able to pass the value of Bills type and it was successfully created.
Here is the console output:
You have 300 coins.
Process finished with exit code 0

ERROR: Phantom-dsl BatchQuery Unspecified with Overloaded method

I am attempting to extend my application to include another Cassandra table for storing the Transactions included in each Block.
I have tried to keep the code snippets succinct and relevant. If there is further code context required - just let me know.
phantomVersion = "1.22.0"
cassandraVersion = "2.1.4"
I am getting the following compilation error with the code listed below. Insights greatly appreciated.
[error] /home/dan/projects/open-blockchain/scanner/src/main/scala/org/dyne/danielsan/openblockchain/data/database/Database.scala:30: overloaded method value add with alternatives:
[error] (batch: com.websudos.phantom.batch.BatchQuery[_])com.websudos.phantom.batch.BatchQuery[com.websudos.phantom.builder.Unspecified] <and>
[error] (queries: Iterator[com.websudos.phantom.builder.query.Batchable with com.websudos.phantom.builder.query.ExecutableStatement])(implicit session: com.datastax.driver.core.Session)com.websudos.phantom.batch.BatchQuery[com.websudos.phantom.builder.Unspecified] <and>
[error] (queries: com.websudos.phantom.builder.query.Batchable with com.websudos.phantom.builder.query.ExecutableStatement*)(implicit session: com.datastax.driver.core.Session)com.websudos.phantom.batch.BatchQuery[com.websudos.phantom.builder.Unspecified] <and>
[error] (query: com.websudos.phantom.builder.query.Batchable with com.websudos.phantom.builder.query.ExecutableStatement)(implicit session: com.datastax.driver.core.Session)com.websudos.phantom.batch.BatchQuery[com.websudos.phantom.builder.Unspecified]
[error] cannot be applied to (scala.concurrent.Future[com.datastax.driver.core.ResultSet])
[error] .add(ChainDatabase.bt.insertNewBlockTransaction(bt))
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 6 s, completed Aug 9, 2016 2:42:30 PM
GenericBlockModel.scala:
case class BlockTransaction(hash: String, txid: String)
sealed class BlockTransactionModel extends CassandraTable[BlockTransactionModel, BlockTransaction] {
override def fromRow(r: Row): BlockTransaction = {
BlockTransaction(
hash(r),
txid(r)
)
}
object hash extends StringColumn(this) with PartitionKey[String]
object txid extends StringColumn(this) with ClusteringOrder[String] with Descending
}
abstract class ConcreteBlockTransactionModel extends BlockTransactionModel with RootConnector {
override val tableName = "block_transactions"
def insertNewBlockTransaction(bt: BlockTransaction): Future[ResultSet] = insertNewRecord(bt).future()
def insertNewRecord(bt: BlockTransaction) = {
insert
.value(_.hash, bt.hash)
.value(_.txid, bt.txid)
}
}
Database.scala
class Database(val keyspace: KeySpaceDef) extends DatabaseImpl(keyspace) {
def insertBlock(block: Block) = {
Batch.logged
.add(ChainDatabase.block.insertNewRecord(block))
.future()
}
def insertTransaction(tx: Transaction) = {
Batch.logged
.add(ChainDatabase.tx.insertNewTransaction(tx))
.future()
}
def insertBlockTransaction(bt: BlockTransaction) = {
Batch.logged
.add(ChainDatabase.btx.insertNewBlockTransaction(bt))
.future()
}
object block extends ConcreteBlocksModel with keyspace.Connector
object tx extends ConcreteTransactionsModel with keyspace.Connector
object btx extends ConcreteBlockTransactionsModel with keyspace.Connector
}
object ChainDatabase extends Database(Config.keySpaceDefinition)
The error is obviously that you are trying to add a Future to a Batch, when a Batch needs a query. If you already triggered a query, it's not possible to batch it anymore, so you need to stop one step ahead. Here's how:
def insertNewRecord(
bt: BlockTransaction
): InsertQuery.Default[BlockTransactionModel, BlockTransaction] = {
insert
.value(_.hash, bt.hash)
.value(_.txid, bt.txid)
}
Now you can add multiple records to a batch with:
Batch.logged.add(insertNewRecord(record1)
.add(insertNewRecord(record2))
// etc
On a different note a batch in Cassandra is not used to do parallel inserts, instead it is used to guarantee atomicity which makes it in general at least 30% slower than a normal parallel insert. Read this for more details.
If you simply want to insert more things at the same time, you can use the method that returns a future like this:
def insertMany(
list: List[BlockTransaction]
): Future[List[ResultSet]] = {
Future.sequence(list.map(insertNewRecord(_).future()))
}

could not find implicit value for parameter messages: play.api.i18n.Messages

I have the following piece of code
import play.api.i18n.{MessagesApi, Messages, I18nSupport}
import play.api.libs.json.Json
case class HttpMessage(key: String, message: String)
object HttpMessage {
implicit val jsonFormat = Json.format[HttpMessage]
def apply(key: String): HttpMessage = {
HttpMessage(key, Messages(key))
}
}
When compiled, it throws
[error] could not find implicit value for parameter messages: play.api.i18n.Messages
[error] HttpMessage(key, messages(key))
[error] ^
I made some research and it seems that it cannot find an implicit value for MessagesAPI. It seems it must be inject like in controllers but I do not know how because I am facing an object and case class here. #Inject annotation is not accepted.
How can I fix this?
Approach from https://stackoverflow.com/a/30843682/4496364 :
import play.api.Play.current
import play.api.i18n.Messages.Implicits._
The first line is deprecated since Play now uses DI everywhere possible.
My approach (can't say if good or bad):
case class HttpMessage(key: String, message: String)
object HttpMessage {
implicit val jsonFormat = Json.format[HttpMessage]
def apply(key: String)(implicit messages: Messages): HttpMessage = {
HttpMessage(key, Messages(key))
}
}
I had to create similar solution, so I used the implicit way, which Play uses also in it's templates. You must have implicit request in your controller for this to work. Also, in all service-like classes you need to forward this implicit messages: Messages...