考虑以下类型定义:
trait LiftF[F[_], G[_]] {
def liftF[A](fa: F[A]): G[A]
}
Run Code Online (Sandbox Code Playgroud)
在上下文边界中提供对此类隐式的要求时(使用种类投影仪插件),我们必须这样编写:
def func[A, G[_], F[_]: LiftF[?[_], G]](a: F[A]): G[A]
Run Code Online (Sandbox Code Playgroud)
我想摆脱这一?[_]部分,所以我最初的猜测是编写一个To[G[_]]返回的类型,LiftF[?[_], G]以将上述函数定义转换为
def func[A, G[_], F[_]: LiftF.To[G]](a: F[A]): G[A]
Run Code Online (Sandbox Code Playgroud)
但是,在将类型To定义写为
type To[G[_]] = LiftF[?[_], G]
Run Code Online (Sandbox Code Playgroud)
我收到以下编译错误:
Error:(17, 20) type ?$ takes type parameters
type To[G[_]] = LiftF[?[_], G]
Run Code Online (Sandbox Code Playgroud)
尝试使用存在性类型重写它会产生以下类型定义:
type To[G[_]] = LiftF[F, G] forSome { type F[X] }
Run Code Online (Sandbox Code Playgroud)
这可以很好地编译,但是,毫无疑问,它不能应用于其他类型参数,因此无法实现所需的函数定义。
我设法通过受aux模式启发的代码实现了“部分应用程序”部分:
trait To[G[_]] {
type From[F[_]] = LiftF[F, G]
}
Run Code Online (Sandbox Code Playgroud)
可悲的是,这给我留下了比原始语法差的语法:
def func[A, …Run Code Online (Sandbox Code Playgroud) types scala partial-application higher-kinded-types kind-projector
我知道我可以使用
import zio.Task
def zip3Par[A, B, C](a: Task[A], b: Task[B], c: Task[C]): Task[(A, B, C)] =
a.zipPar(b).zipWithPar(c) { case ((a, b), c) => (a, b, c) }
def zip4Par[A, B, C, D](a: Task[A], b: Task[B], c: Task[C], d: Task[D]): Task[(A, B, C, D)] =
zip3Par(a, b, c).zipWithPar(d) { case ((a, b, c), d) => (a, b, c, d) }
Run Code Online (Sandbox Code Playgroud)
并行执行 3 或 4 个任务,但如果有更优雅的解决方案,我会更受伤吗?
我想在我的ScalaTests中使用camunda-bpm-assert-scenario。
我在这里有以下代码receiveTask::receive:
when(documentRequest.waitsAtReceiveTask("ReceiveTaskWaitForDocuments")).thenReturn((receiveTask) -> {
receiveTask.defer("P1DT1M", receiveTask::receive);
});
Run Code Online (Sandbox Code Playgroud)
根据中的答案,是否可以在Scala中使用Java 8样式方法引用?我可以很容易地将其翻译为:
receiveTask.defer("P1D", receiveTask.receive _)
Run Code Online (Sandbox Code Playgroud)
但这给了我:
Error:(84, 45) type mismatch;
found : Unit
required: org.camunda.bpm.scenario.defer.Deferred
receiveTask.defer("P1D", receiveTask.receive _)
Run Code Online (Sandbox Code Playgroud)
这是receive功能:
void receive();
Run Code Online (Sandbox Code Playgroud)
这是预期的接口:
public interface Deferred {
void execute() throws Exception;
}
Run Code Online (Sandbox Code Playgroud)
如何在Scala中实现这一目标?这不是Scala中可以使用Java 8样式方法引用的副本吗?,则无法解决“错误:(84,45)类型不匹配; ...”
根据cats官方文档:https : //typelevel.org/cats-effect/typeclasses/liftio.html ,如果我们想把东西从IO提升到其他容器,你应该实现LiftIO trait,但示例明确运行unsafeRunXXX方法来获取出了效果,我想知道这是转型的唯一途径吗?
我有一个表面上很简单的宏观问题,我已经用头撞了几个小时,但没有运气。也许有更多经验的人可以提供帮助。
我有以下宏:
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
object MacroObject {
def run(s: String): Unit =
macro runImpl
def runImpl(c: Context)(s: c.Tree): c.Tree = {
import c.universe._
println(s) // <-- I need the macro to know the value of s at compile time
q"()"
}
}
Run Code Online (Sandbox Code Playgroud)
问题是:我希望宏知道s传递给它的值——不是 AST s,而是s它本身的值。具体来说,我希望它具有这种行为:
def runTheMacro(str: String): Unit = MacroObject.run(str)
final val HardCodedString1 = "Hello, world!"
runTheMacro(HardCodedString1) // the macro should print "Hello, world!"
// to the console during macro expansion …Run Code Online (Sandbox Code Playgroud) 在 Scala 反射中,通常可以使用 TypeCreator 从 Type 构造 TypeTag:
object TypeUtils {
import ScalaReflection.universe._
def createTypeTag[T](
tpe: Type,
mirror: reflect.api.Mirror[reflect.runtime.universe.type]
): TypeTag[T] = {
TypeTag.apply(
mirror,
NaiveTypeCreator(tpe)
)
}
case class NaiveTypeCreator(tpe: Type) extends reflect.api.TypeCreator {
def apply[U <: reflect.api.Universe with Singleton](
m: reflect.api.Mirror[U]): U#Type = {
// assert(m eq mirror, s"TypeTag[$tpe] defined in $mirror cannot be migrated to $m.")
tpe.asInstanceOf[U#Type]
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,事实证明 的输出createTypeTag不可序列化,这与编译时推理创建的 typeTag 不同:
import java.io.{ByteArrayOutputStream, ObjectOutputStream}
import org.apache.spark.sql.catalyst.ScalaReflection
import org.scalatest.FunSpec
class TypeTagFromType extends FunSpec { …Run Code Online (Sandbox Code Playgroud) 我将使用很棒的库https://tpolecat.github.io/doobie/,它功能齐全。
我正在经历第一个例子,我已经认识到:
Transactor 是一种数据类型,它知道如何连接到数据库、分发连接并清理它们;有了这些知识,它可以转换
ConnectionIO ~> IO,这给了我们一个可以运行的程序。
ConnectionIO ~> IO 是范畴论中的自然变换,但从未完全理解,究竟什么是自然变换。
但是,我知道这是从一类到另一类的转变。例如:
F[A] ~> G[A]
Run Code Online (Sandbox Code Playgroud)
是从类别F到G不改变内容的自然转变。
并非一切都可以自然转化,问题是,library doobie 的作者怎么知道,他可以从 进行自然转化ConnectionIO ~> IO?
haskell functional-programming scala category-theory parametric-polymorphism
这是我的代码的简化版本。
我怎样才能避免打电话asInstanceOf(因为这是设计不佳的解决方案的味道)?
sealed trait Location
final case class Single(bucket: String) extends Location
final case class Multi(buckets: Seq[String]) extends Location
@SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf"))
class Log[L <: Location](location: L, path: String) { // I prefer composition over inheritance
// I don't want to pass location to this method because it's a property of the object
// It's a separated function because there is another caller
private def getSinglePath()(implicit ev: L <:< Single): String = s"fs://${location.bucket}/$path"
def getPaths(): Seq[String] =
location match { …Run Code Online (Sandbox Code Playgroud) 我正在尝试copy()一个具有类型参数的 Scala 案例类。在呼叫站点,该值的类型为Foo[_]。
这按预期编译:
case class Foo[A](id: String, name: String, v1: Bar[A])
case class Bar[A](v: A)
val foo: Foo[_] = Foo[Int]("foo1", "Foo 1", Bar[Int](1))
foo.copy(id = "foo1.1")
Run Code Online (Sandbox Code Playgroud)
但是,如果我添加另一个 type 成员Bar[A],它将不再编译:
case class Foo[A](id: String, name: String, v1: Bar[A], v2: Bar[A])
case class Bar[A](v: A)
val foo: Foo[_] = Foo[Int]("foo1", "Foo 1", Bar[Int](1), Bar[Int](2))
foo.copy(id = "foo1.1") // compile error, see below
Run Code Online (Sandbox Code Playgroud)
type mismatch;
found : Playground.Bar[_$1]
required: Playground.Bar[Any]
Note: _$1 <: Any, but class …Run Code Online (Sandbox Code Playgroud) 我在 Scala 3 中定义了以下特征:
\ntrait A[T <: Tuple]\nRun Code Online (Sandbox Code Playgroud)\n然后,我使用 Scala 3 宏创建具有此特征的对象,对元组的实际类型执行进一步检查T;特别是,我想检查元组的所有类型(T_1,\xe2\x80\xa6,T_n)T是否是另一个给定类型的子类型B:
trait B\nprivate def allSubtypesOfB[T <: Tuple: Type](using quotes: Quotes): Boolean = {\n import quotes.reflect.*\n case '[Nothing] => false // I don't want nothing to be in T\n case '[head *: tail] if TypeRepr.of[head] <:< TypeRepr.of[B] => allSubtypesOfB[tail]\n case '[EmptyTuple] => true\n case _ => false\n}\n\ninline def createA[T <: Tuple] = ${ createAImpl[T] }\nprivate def createAImpl[T …Run Code Online (Sandbox Code Playgroud) scala ×10
generics ×2
scala-macros ×2
types ×2
camunda ×1
case-class ×1
casting ×1
cats-effect ×1
haskell ×1
implicit ×1
io-monad ×1
java ×1
lambda ×1
macros ×1
reflection ×1
scala-3 ×1
scala-cats ×1
scalaz ×1
tuples ×1
typeclass ×1
zio ×1