scala-implicits

Caching implicit resolution

一笑奈何 提交于 2019-12-20 14:12:06
问题 To reduce compile times of my project, I'm caching certain type classes that are resolved by implicit lookups. This appears somewhat cumbersome though, because the straight forward implementation does not work: scala> implicit val x: String = implicitly[String] x: String = null The implicit lookup considers its own, uninitialized definition as a valid implementation. A lazy val would blow the stack with infinite recursion. Therefore I'm currently handling it in this fashion: implicit val x:

scala typing require implicit

烈酒焚心 提交于 2019-12-13 18:55:24
问题 I'm trying to build following I have a parent generic class abstract class ResultProvider[+T: Writes](db: DB) { def get(id: Long): Future[Seq[T]] } And some implementations, e.g. class LengthProvider(db: DB) extends ResultProvider[LengthResult](db){ override def get (userId: Long): Future[Seq[LengthResult]] = ... } object LengthProvider extends ((DB) => DisciplinePredictor) { override def apply(db: DB) = new LengthProvider(db) } I have following configuration map: val providers: Map[String, (

Scala implicit conversion for object

夙愿已清 提交于 2019-12-12 04:55:33
问题 Suppose that I have the following code snippet: import scala.language.implicitConversions sealed trait Command { val typeName: String } object Command { implicit def command2String(implicit c: Command): String = c.typeName } case object SendMessageCommand extends Command { override val typeName: String = "send_message" } I would like to compare String with Command descendants without explicit conversion. For example: "sendMessage" == SendMessageCommand Q1: Is it possible in Scala? Q2: Can I

Best way to handle false unused imports in intellij

痞子三分冷 提交于 2019-12-09 15:18:06
问题 Intellij falsely marked some import of Scala implicits as not being use. Is there a way to prevent it from deleting those import when optimized them explicitly for a specific import and not prevent optimized import for the entire project ? 回答1: I'm afraid there isn't, I had similar issues especially when using akka and importing the implicit execution context from an ActorSystem in some cases. I recommend defining the value instead of importing. One such example would be: // Avoid importing

How to implement Functor[Dataset]

被刻印的时光 ゝ 提交于 2019-12-06 06:03:23
问题 I am struggling on how to create an instance of Functor[Dataset] ... the problem is that when you map from A to B the Encoder[B] must be in the implicit scope but I am not sure how to do it. implicit val datasetFunctor: Functor[Dataset] = new Functor[Dataset] { override def map[A, B](fa: Dataset[A])(f: A => B): Dataset[B] = fa.map(f) } Of course this code is throwing a compilation error since Encoder[B] is not available but I can't add Encoder[B] as an implicit parameter because it would

How to implement Functor[Dataset]

痞子三分冷 提交于 2019-12-04 10:45:10
I am struggling on how to create an instance of Functor[Dataset] ... the problem is that when you map from A to B the Encoder[B] must be in the implicit scope but I am not sure how to do it. implicit val datasetFunctor: Functor[Dataset] = new Functor[Dataset] { override def map[A, B](fa: Dataset[A])(f: A => B): Dataset[B] = fa.map(f) } Of course this code is throwing a compilation error since Encoder[B] is not available but I can't add Encoder[B] as an implicit parameter because it would change the map method signature, how can I solve this? You cannot apply f right away, because you are

Best way to handle false unused imports in intellij

戏子无情 提交于 2019-12-04 03:13:29
Intellij falsely marked some import of Scala implicits as not being use. Is there a way to prevent it from deleting those import when optimized them explicitly for a specific import and not prevent optimized import for the entire project ? I'm afraid there isn't, I had similar issues especially when using akka and importing the implicit execution context from an ActorSystem in some cases. I recommend defining the value instead of importing. One such example would be: // Avoid importing the execution context like this class MyActor extends Actor { import context.system.dispatcher } // Define it

Caching implicit resolution

隐身守侯 提交于 2019-12-03 02:52:23
To reduce compile times of my project, I'm caching certain type classes that are resolved by implicit lookups. This appears somewhat cumbersome though, because the straight forward implementation does not work: scala> implicit val x: String = implicitly[String] x: String = null The implicit lookup considers its own, uninitialized definition as a valid implementation. A lazy val would blow the stack with infinite recursion. Therefore I'm currently handling it in this fashion: implicit val x: String = cache.x object cache { val x: String = implicitly[String] } But this makes it overly