Right now trying to instantiate a new JSONConverter to register Jackson's Scala module.
private def getConverter(implicit m: ClassTag[T]) = {
new JSONConverter[T](classTag[T].runtimeClass, bucketName)
JSONConverter.registerJacksonModule(DefaultScalaModule)
converter
}
The above code sits in a standard Scala trait that looks like trait Writeable[T] { }.
The problem with the above code is that Scala seems to be having a difficult time with Types. Compiler error is:
[error] found : Class[_$1] where type _$1
[error] required: Class[T]
[error] val converter = new JSONConverter[T](classTag[T].runtimeClass, bucketName(clientId))
[error] ^
[error] one error found
Anyone know the source or easy fix of this issue? Thanks!
Update
Although #wingedsubmariner had an answer that allowed this to originally compile, as soon as I went to write more code the issue cascaded further. I'll show an example:
val o = bucketLookup(clientId).fetch(id, classTag[T].runtimeClass).withConverter(converter).withRetrier(DB.retrier).r(DB.N_READ).execute()
At withConverter the compiler throws the same error:
[error] found : com.basho.riak.client.convert.JSONConverter[T]
[error] required: com.basho.riak.client.convert.Converter[_$1] where type _$1
[error] val o = bucketLookup(clientId).fetch(id, classTag[T].runtimeClass).withConverter(converter).withRetrier(DB.retrier).r(DB.N_READ).execute()
I even tried doing the same type casting using converter.asInstanceOf[JSONConverter[T]] but inheritance (JSONConverter<T> extends Converter<T>) seems to cascade the issue. Any ideas here?
runtimeClass is retuning a Class with the wrong type parameter. Try:
new JSONConverter(classTag[T].runtimeClass.asInstanceOf[Class[T]], bucketName(clientId))
Related
I have a Vars binding statement, like so
val data: Vars[Contact] = Vars.empty[Contact]
I'm trying to show the number of elements like so:
<div>{data.all.bind.size}</div>
But this produces a complication error
type mismatch;
[error] found : com.thoughtworks.binding.Binding[App.this.data.All[App.this.Contact]]
[error] (which expands to) com.thoughtworks.binding.Binding[scala.collection.mutable.Buffer[_ <: App.this.Contact]]
[error] required: com.thoughtworks.binding.Binding[scala.collection.mutable.Buffer[_$6]] where type _$6 <: App.this.Contact
[error] {this.data.all.bind.size}</div>
How to make this work?
Update
trying to use String as type for Vars binding, same outcome
type mismatch;
[error] found : com.thoughtworks.binding.Binding[App.this.data.All[String]]
[error] (which expands to) com.thoughtworks.binding.Binding[scala.collection.mutable.Buffer[_ <: String]]
[error] required: com.thoughtworks.binding.Binding[scala.collection.mutable.Buffer[_$6]] where type _$6 <: String
[error] {this.data.all.bind.size}</div>
Note that i'm using Scala.js 1.2, Scala 2.13.3 and
libraryDependencies += "org.lrng.binding" %%% "html" % "latest.release"
unfortunately, can't provide scalafiddle since it doesn't support Scala 2.13, instead I created a replica project here-
https://www.dropbox.com/s/i9dpsa9pz0lejtj/bindingreplica.tar.gz?dl=0
You can use length for this.
<div>{data.length.bind.toString}</div>
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 have been trying to create a Generic Dao over Slick 3.1.1 and it includes a generic filter that competes with JPA's findByExample, see the following files:
GenericDaoImpl.scala Generic level reusable across all Models
UserDao.scala Generic plus customizations for the User model
UserService.scala Wraps the UserDao into more services level functionality
In this last file I try to use the generic filter function to find a user by its registered email, like this:
// this will implicitly exec and wait indefinitely for the
// db.run Future to complete
import dao.ExecHelper._
def findByEmail(email: String): Option[UserRow] = {
userDao.filter(_.email === email).headOption
}
but this produces the compiler error:
[error] /home/bravegag/code/play-authenticate-usage-scala/app/services/UserService.scala:35: value === is not a member of String
[error] userDao.filter(email === _.email).headOption
[error] ^
[error] /home/bravegag/code/play-authenticate-usage-scala/app/services/UserService.scala:35: ambiguous implicit values:
[error] both value BooleanOptionColumnCanBeQueryCondition in object CanBeQueryCondition of type => slick.lifted.CanBeQueryCondition[slick.lifted.Rep[Option[Boolean]]]
[error] and value BooleanCanBeQueryCondition in object CanBeQueryCondition of type => slick.lifted.CanBeQueryCondition[Boolean]
[error] match expected type slick.lifted.CanBeQueryCondition[Nothing]
[error] userDao.filter(email === _.email).headOption
[error] ^
Can anyone advice on how the implicit declaration of the filter function below can be improved to solve this compiler error?
The implementation of the filter function (found in GenericDaoImpl.scala) is:
// T is defined above as T <: Table[E] with IdentifyableTable[PK]
override def filter[C <: Rep[_]](expr: T => C)
(implicit wt: CanBeQueryCondition[C]) : Future[Seq[E]] =
db.run(tableQuery.filter(expr).result)
As far as I can see you are simply lacking you profile API import in UserService.
Just add there this import: import profile.api._ and it should work.
EDIT: BTW I see many people building their own version of base CRUDs for Slick. Did you try some existing thin libraries doing just that e.g. here: https://github.com/VirtusLab/unicorn ? It's not really related to this question but it may be worth to take a look.
I am trying to implement Phantom Reference in Scala to replace finalize(). I have a file object, which needs to be GC'ed using Phantom Reference. While there are some code samples in java, I am not able to find anything in Scala. I did try writing in Scala like this :
val q = new ReferenceQueue()
val phantom = new PhantomReference(file,q)
But I am getting the following error
found : java.lang.ref.ReferenceQueue[Nothing]
[error] required: java.lang.ref.ReferenceQueue[_ >: java.io.File]
[error] Note: Nothing <: Any, but Java-defined class ReferenceQueue is invariant in type T.
[error] You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
[error] val phantom = new PhantomReference(file,q)
I understand I am missing something trivial, but I am not very proficient in Scala. Can someone help?
Because of the way type inference works in Scala, q is inferred to be of type java.lang.ref.ReferenceQueue[Nothing], but you want it to be a java.lang.ref.ReferenceQueue[File], so you need to make this explicit when you create it:
val q = new ReferenceQueue[File]()
I'm currently doing that most noble of programming endeavors, writing tests for Json encoding / decoding. I'm using Argonaut.io for Json and Scalatest for my testing framework. Under scalatest, the use of === during assertion verification provides additional information if a failure occurs, while the use of ==simply gives this org.scalatest.exceptions.TestFailedException was thrown.. However, the scala compiler is not happy. Here's the code:
val default = new Broadcast("default", "default", "default")
test("Should parse out network when present") {
val hcursor = testHCursor(jsonPath + "complete-broadcast.json")
val actualNetwork = Parser.BroadcastDecodeJson(hcursor)
.getOr(default)
.network
assert(actualNetwork === "ESPNU")
}
That spews out this:
[info] Compiling 1 Scala source to /home/vagrant/waltercamp/waltercamp-dataservice/target/scala-2.10/test-classes...
[error] /home/vagrant/waltercamp/waltercamp-dataservice/src/test/scala/io/ptx/waltercamp/schedules/BroadcastParserSuite.scala:16: type mismatch;
[error] found : actualNetwork.type (with underlying type String)
[error] required: ?{def ===(x$1: ? >: String("ESPNU")): ?}
[error] Note that implicit conversions are not applicable because they are ambiguous:
[error] both method ToEqualOps in trait ToEqualOps of type [F](v: F)(implicit F0: scalaz.Equal[F])scalaz.syntax.EqualOps[F]
[error] and method convertToEqualizer in trait Assertions of type (left: Any)BroadcastParserSuite.this.Equalizer
[error] are possible conversion functions from actualNetwork.type to ?{def ===(x$1: ? >: String("ESPNU")): ?}
[error] assert(actualNetwork === "ESPNU")
[error] ^
[error] one error found
[error] (test:compile) Compilation failed
The use == however provides a clean compilation and pass. Is there a way provide the compiler a hint as to which conversion, or the conversion order, to use?
I'd go with ScalaTest's version here. One approach would be to apply the conversion explicitly:
assert(convertToEqualizer(actualNetwork) === "ESPNU")
That's kind of unpleasant, though, and involves a lot of repetitious boilerplate if you're using === many times in a file. Another way would be to exclude the Scalaz conversion from the general import:
import scalaz._, Scalaz.{ ToEqualOps => _, _ }
You could also switch to à la carte imports for Scalaz and just be sure you don't pull in ToEqualOps via scala.syntax.equal._. I'll admit I find à la carte imports a pain to maintain sometimes, but if you're not doing much with Scalaz in the test this wouldn't be too bad.