简体   繁体   English

我怎样才能使隐式字段成为瞬态的?

[英]How can I make an implicit field, transient?

I want to set up doobie inside the AsyncFunction of apache Flink but it needs an implicit read instance of output.我想在 apache Flink 的 AsyncFunction 中设置 doobie,但它需要一个隐式读取输出实例。 on the other hand, I can not send Read[Out] as an implicit parameter because the AsyncFunction needs to be serializable and this implicit value violates this rule:另一方面,我不能将 Read[Out] 作为隐式参数发送,因为 AsyncFunction 需要是可序列化的,而这个隐式值违反了这条规则:

package FlinkHelpers
import Configs.JdbConfig
import doobie.hikari._
import doobie._
import doobie.implicits._
import doobie.util.ExecutionContexts
import cats._
import cats.data._
import cats.effect.IO.asyncForIO
import cats.effect._
import cats.implicits._
import fs2.Stream
import cats.effect.unsafe.implicits.global
import org.apache.flink.runtime.concurrent.Executors
import org.apache.flink.streaming.api.scala.async.{AsyncFunction, ResultFuture}

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try
class AsyncDatabaseRequestOne[In,Out](configs:JdbConfig)(implicit r:Read[Out]) extends AsyncFunction[In,(In, Out)] {
  lazy val transactor: Resource[IO, HikariTransactor[IO]] = {
    for {
      ce <- ExecutionContexts.fixedThreadPool[IO](32)
      xa <- HikariTransactor.newHikariTransactor[IO](
        "org.postgresql.Driver",
        "jdbc:postgresql://localhost:5433/doobie",
        "psql",
        "110271",
        ce
      )
    } yield xa
  }
  /** The context used for the future callbacks */
  implicit lazy val executor: ExecutionContext = ExecutionContext.fromExecutor(Executors.directExecutor())

  override def asyncInvoke(input: In, resultFuture: ResultFuture[(In, Out)]): Unit = {
    val v2: IO[Out] = transactor.use[Out] { xa2: HikariTransactor[IO] => {
      sql"select code,name,population,gnp from Out limit 1"
        .query[Out] // Query0[String]
        .unique // ConnectionIO[List[String]]
        .transact(xa2) // IO[List[String]]
    }
    }
    val future = v2.unsafeToFuture()
    future.onSuccess({
      case result: Out => resultFuture.complete(List((input, result)))
    })
  }
}

on the other hand scala does not allow me to make the r field transiend does anyone have any idea how I can solve this?另一方面,scala 不允许我使r字段变态,有谁知道我该如何解决这个问题吗?

r in class AsyncDatabaseRequestOne[In,Out](configs:JdbConfig)(r:Read[Out])... is a constructor parameter but Scala can generate private[this] field for it too:r class AsyncDatabaseRequestOne[In,Out](configs:JdbConfig)(r:Read[Out])...中的 r 是构造函数参数,但 Scala 也可以为其生成private[this]字段:

Disambiguate constructor parameter with same name as class field of superclass ( answer ) 消除与超类的类字段同名的构造函数参数的歧义答案

Try to add annotation @transient尝试添加注解@transient

class AsyncDatabaseRequestOne[In,Out](configs:JdbConfig)(@transient private[this] implicit val r:Read[Out])...

or要么

class AsyncDatabaseRequestOne[In,Out](configs:JdbConfig)(@(transient @field) private[this] implicit val r:Read[Out])...

Since @transient is defined already with @field因为@transient已经用@field定义了

@field
final class transient extends scala.annotation.StaticAnnotation

I guess in our case @field meta-annotation is not necessary.我想在我们的例子中@field元注释是没有必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在匿名函数中隐式设置多个参数? - How can I make multiple parameters in an anonymous function implicit? Scala如何隐式使抽象方法成为有效参数,以便可以在Java中使用方法 - How scala implicit make abstract method valid parameter so I can use method in java 如何使“这个”含蓄? - How to make “this” implicit? 我可以让Scala更喜欢隐式转换为Java 8 Lambda吗? - Can I make Scala prefer an implicit conversion to a Java 8 Lambda? 如何访问隐式“隐式”,即def a [A:B]或def a [A &lt;%B]? - How can I access implicit “implicit” i.e. def a[A :B] or def a[A <% B]? 如何消除该点? 我正在测试隐式转换 - How Can I eliminate this dot? I was testing Implicit Conversion 我可以告诉scala如何更喜欢更具体的隐含,而不是给出“含糊不清的”错误吗? - Can I tell scala how to prefer more specific implicit, rather than give “ambiguous implicit” error? 如何使用类型依赖于隐式参数的方法参数? - How can I have a method parameter with type dependent on an implicit parameter? 如何在 Scala 中找到隐式的来源? - How can I locate where an implicit comes from in Scala? Scala和Play:如何使用我的隐式对象? - Scala and Play: how can I use my implicit object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM