The following statement in Scala throws a Runtime error:
val a = 10: Object
error: the result type of an implicit conversion must be more specific than Object
val a = 10: Object
^
If the above-mentioned implicit conversion is not allowed in Scala, then why doesn't Scala throws the error at compile time?
Why do you think this is a runtime error? It is not:
$ scalac impl.scala
impl.scala:3: error: the result type of an implicit conversion must be more specific than Object
val a = 10: Object
^
one error found
Related
I tried the code of this Blog: a-streaming-library-with-a-superpower-fs2-and-functional-programming
In Intellij this code:
Stream("bob", "alice", "joe")
.evalMap(name => IO.fromFuture(IO(loadUserIdByName(name)))) // <- here is the exception
.compile
.toList
Gives me this exception:
Expression of type IO[Long] doesn't conform to expected type FS2_[O2_]
Running the code with sbt works without a problem.
Is there a way to get rid of this exception in Intellij?
I noticed myself the typechecker always needs some help here, so explicitly set the effect and return type: in your case evalMap[IO, Long]
I have a case class defined as case class IndexList(value: ListBuffer[String])
I'm unable to convert this to JSON using Play API.
I've tried
object IndexList{
implicit val indexListWrites = Json.writes[IndexList]
}
as part of Macro Inception but it fails giving compilation error:
exception during macro expansion:
[error] java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.tl$1()Lscala/collection/immutable/List;
I've also tried
object IndexList{
implicit val indexListWrites: Writes[IndexList] = Writes{
(indexList: IndexList) => JsArray(indexList.value)
}
}
but fails with error:
type mismatch;
[error] found : scala.collection.mutable.ListBuffer[String]
[error] required: Seq[play.api.libs.json.JsValue]
Any help regarding either of the methods would be helpful.
If there are other ways to accomplish this, it's fine as well.
I've just got an issue with the BluePrints API.
I've executed the following Scala commands:
val dbBasename = "C:\\Users\\taatoal1\\tmp\\orientdb\\databases\\"
val dbpath = "test_ingest"
val (uname, pwd) = ("admin", "admin")
val graph = new OrientGraph(s"plocal:$dbpath", uname, pwd)
graph.addVertex("class:Employee")
and I got the following error
<console>:14: error: ambiguous reference to overloaded definition,
both method addVertex in class OrientBaseGraph of type (x$1: Any, x$2: <repeated...>[Object])com.tinkerpop.blueprints.impls.orient.OrientVertex
and method addVertex in class OrientBaseGraph of type (x$1: Any)com.tinkerpop.blueprints.impls.orient.OrientVertex
match argument types (String)
graph.addVertex("class:Employee")
^
Do you have any idea what I did wrong?
Thanks in advance
In the end I found out that there is another version of addVertex that takes two strings as parameters: the class name and the cluster name.
I'm using a parameter for my ramp value as per the docs,
val rampUpRate = Integer.getInteger("ramp", 1)
setUp(
scn.users(10).ramp(rampUpRate).protocolConfig(httpConf)
)
But when I run gatling, I'm getting an error:
09:57:35.695 [ERROR] c.e.e.g.a.ZincCompiler$ - /Gatling/user-files/simulations/clients/com/mydomain/www/stress/RecordedSimulation.scala:1088: overloaded method value ramp with alternatives:
(duration: akka.util.Duration)com.excilys.ebi.gatling.core.scenario.configuration.ConfiguredScenarioBuilder <and>
(duration: Long)com.excilys.ebi.gatling.core.scenario.configuration.Configured
ScenarioBuilder
cannot be applied to (java.lang.Integer)
I thought I could simply cast to Long before using the parameter
val rampUpRate = Integer.getInteger("ramp", 1)
setUp(
scn.users(10).ramp((Long) rampUpRate).protocolConfig(httpConf)
)
but this still errors:
09:57:35.695 [ERROR] c.e.e.g.a.ZincCompiler$ - /Gatling/user-files/simulations/clients/com/mydomain/www/stress/RecordedSimulation.scala:1088: \sanctuarySpa\com\sanctuaryspa\www\stress\RecordedSimulation.scala:1088:
value rampUpRate is not a member of object Long
10:05:34.915 [ERROR] c.e.e.g.a.ZincCompiler$ - scn1.users(10).ramp((Long) rampUpRate).protocolConfig(httpConf),
Any suggestions why following the documentation, or the explicit cast to long don't work?
Try using rampUpRate.toLong to cast to a Long (or the more general cast rampUpRate.asInstanceOf[Long])
(Long) rampUpRate is seen by the compiler as trying to perform Long.rampUrRate() e.g. applying function rampUpRate to object Long, hence the error message
That's my fault: the wiki page is not up to date.
What happens is that you have a java.lang.Integer while the method takes a scala Long. java.lang.Long can be implicitly converted into scala Long, but not java.lang.Integer.
The proper way would be val rampUpRate = java.lang.Long.getLong("ramp", 1L)
PS: I've fixed the doc.
I'm trying to load a bunch of com.mongodb.DBObject objects in to a Vaadin BeanItemContainer to display in a table. I'm getting stuck on the translation of the constructor from Java to Scala.
The constructor definition is:
BeanItemContainer(Class<? extends BT> type)
This passes the scala compiler:
val bic = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))
However, when I try to add an item:
mtl.toArray.foreach {t => bic.addBean(t)}
I get the following error:
[ERROR]com/sentientswarm/traderdashboard/UploadTradesWindow.scala:140: error: type mismatch;
found : t.type (with underlying type com.mongodb.DBObject)
required: ?0 where type ?0
mtl.toArray.foreach {t => bic.addBean(t)}
Any thoughts/suggestions?
UPDATE:
Tried:
val bic: BeanItemContainer[DBObject] = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))
Result:
[ERROR]com/sentientswarm/traderdashboard/UploadTradesWindow.scala:140: error: type mismatch;
found : java.lang.Class[?0(in value bic)] where type ?0(in value bic)
required: java.lang.Class[_ <: com.mongodb.DBObject]
val bic: BeanItemContainer[DBObject] = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))
^
Thanks,
John
Any reason you're using Class.forName? I don't think the compiler can infer the type from the returned object from that call, it would just be Class[_]. If you use classOf, it should let the compiler determine the type:
val bic = new BeanItemContainer[DBObject](classOf[DBObject]))
In other words: DBObject.class in Java translates to classOf[DBObject] in Scala.
Try this:
val bic: BeanItemContainer[BT] = new BeanItemContainer(Class.forName("com.mongodb.DBObject"))
By the way, you removed the "^" marker of where in the line the error is. Please, keep it when pasting error messages.