我找不到以下语法规则的解释:
\nFunType ::= FunTypeArgs (\xe2\x80\x98=>\xe2\x80\x99 | \xe2\x80\x98?=>\xe2\x80\x99) Type\n
Run Code Online (Sandbox Code Playgroud)\n 目前,dotty 编译器发布了它的第一个候选版本,并将在未来几个月内发布。我将 IntelliJ scala 插件更新到 EarlyAccess 以使其与 Scala 3.0 一起使用,它的语法突出显示问题已解决,但仍然存在 IntelliSense 问题,建议仅是 Scala 2.13 API,它不知道 Scala 3.0 API 和它使开发变得困难。我尝试手动添加 Scala 3.0 SDK,但它不接受(因为新版本中的编译器 jar 文件名已更改)。
是否有可能将 Scala 3.0 SDK 添加到 IntelliJ?
implicit
Scala 2 中的关键字和Scala 3 中的given
+有什么区别?using
是否只是implicit
被分成了两个关键字,或者语义也不同,如果是,是如何不同的?
给定的
trait Int // proper type
trait List[A] // 1st-order-kinded type constructor
trait Functor[F[_]] // higher-order-kinded type constructor taking type constructor
trait I[H[F[_]]] // higher-order-kinded type constructor taking higher-order type constructor that takes 1st-order type constructor
Run Code Online (Sandbox Code Playgroud)
与声明的类型参数的类型相比,我们不能传递不同类型的类型参数
scala> def f[F[_[_[_]]]] = 42
def f[F[_$1]] => Int
scala> f[I]
val res5: Int = 42
scala> f[Functor]
1 |f[Functor]
| ^
| Type argument Functor does not conform to upper bound [_$1[_$2]] =>> Any
Run Code Online (Sandbox Code Playgroud)
但是我们可以通过以下方式声明类型参数是多态的 AnyKind
scala> def f[A <: AnyKind] = …
Run Code Online (Sandbox Code Playgroud) 在这个博客中已经很好地解释了在 Scala 3 编译时提取案例类的元素的名称和类型:https : //blog.philipp-martini.de/blog/magic-mirror-scala3/
但是,同一个博客用于productElement
获取存储在实例中的值。我的问题是如何直接访问它们?考虑以下代码:
case class Abc(name: String, age: Int)
inline def printElems[A](inline value: A)(using m: Mirror.Of[A]): Unit = ???
val abc = Abc("my-name", 99)
printElems(abc)
Run Code Online (Sandbox Code Playgroud)
如何(更新和的签名printElems
)实现,printElems
以便printElems(abc)
将其扩展为如下所示:
println(abc.name)
println(abc.age)
Run Code Online (Sandbox Code Playgroud)
或者至少这个:
println(abc._1())
println(abc._2())
Run Code Online (Sandbox Code Playgroud)
但不是这个:
println(abc.productElement(0))
println(abc.productElement(1))
Run Code Online (Sandbox Code Playgroud)
不用说,我正在寻找一种适用于任意 case 类的解决方案,而不仅仅是Abc
. 此外,如果必须使用宏,那就没问题了。但请只使用 Scala 3。
如何在 Scala 3 中使用 akka ?使用 scala 3 时找不到 akka 依赖项
sbt 错误:
[error] not found: /Users/admin/.ivy2/localcom.typesafe.akka/akka-actor-typed_3/2.6.15/ivys/ivy.xml
[error] not found: https://repo1.maven.org/maven2/com/typesafe/akka/akka-actor-typed_3/2.6.15/akka-actor-typed_3-2.6.15.pom
[error] (ssExtractDependencies) sbt.librarymanagement.ResolveException: Error downloading com.typesafe.akka:akka-actor-typed_3:2.6.15
[error] Not found
[error] Not found
[error] not found: /Users/admin/.ivy2/localcom.typesafe.akka/akka-actor-typed_3/2.6.15/ivys/ivy.xml
[error] not found: https://repo1.maven.org/maven2/com/typesafe/akka/akka-actor-typed_3/2.6.15/akka-actor-typed_3-2.6.15.pom
Run Code Online (Sandbox Code Playgroud) 我想要enumDescr
为任何 Scala 3 枚举提供一个简单的函数。
例子:
@description(enumDescr(InvoiceCategory))
enum InvoiceCategory:
case `Travel Expenses`
case Misc
case `Software License Costs`
Run Code Online (Sandbox Code Playgroud)
在Scala 2中这很简单 ( Enumeration
):
def enumDescr(enum: Enumeration): String =
s"$enum: ${enum.values.mkString(", ")}"
Run Code Online (Sandbox Code Playgroud)
但是Scala 3中是如何完成的:
def enumDescr(enumeration: ??) = ...
Run Code Online (Sandbox Code Playgroud) 考虑以下注释:
// ok to have more meta
@field
@param
@compileTimeOnly("Only for code generation")
case class Annot(value: String) extends ConstantAnnotation
Run Code Online (Sandbox Code Playgroud)
现在三种用途:
case class A(x: Int, @Annot("z") y: String)
object A:
def f1(x: Int, y: String @Annot("z")): A = ???
def f2(x: Int, @Annot("z") y: String): A = ???
Run Code Online (Sandbox Code Playgroud)
我想使用 Scala 3 宏来查找每个注释。
Symbol.caseFields
为我提供参数列表,并且在每个参数(类型为Symbol
)上,方法annotations
为我提供了我正在寻找的内容。ValDef
. 如果param.tpt.tpe
匹配,AnnotatedType(tpe, t)
那么t
就是我正在寻找的注释。知道如何获取为方法中的参数提供的注释吗?当我打印术语/符号/树/...时,在这种情况下我什至看不到“z”。
我正在尝试通过 Scala 3.0.0-M2 中的宏获取函数名称我提出的解决方案使用 TreeAccumulator
import scala.quoted._
inline def getName[T](inline f: T => Any): String = ${getNameImpl('f)}
def getNameImpl[T](f: Expr[T => Any])(using Quotes): Expr[String] = {
import quotes.reflect._
val acc = new TreeAccumulator[String] {
def foldTree(names: String, tree: Tree)(owner: Symbol): String = tree match {
case Select(_, name) => name
case _ => foldOverTree(names, tree)(owner)
}
}
val fieldName = acc.foldTree(null, Term.of(f))(Symbol.spliceOwner)
Expr(fieldName)
}
Run Code Online (Sandbox Code Playgroud)
调用此代码时会生成函数的名称:
case class B(field1: String)
println(getName[B](_.field1)) // "field1"
Run Code Online (Sandbox Code Playgroud)
我想知道这是否可以使用引号以更简单的方式完成。
我想实现proxy
一些特征A
(例如委托方法调用到一些 rpc 调用),像这样
def clientProxy[A](using Type[A], Quotes): Expr[A] = {
import quotes.reflect._
val defTrees: List[Tree] = TypeRepr.of[A].typeSymbol.memberFields.collect {
case mf if mf.isDefDef =>
???
}
val exprs = Expr.ofList(defTrees.map(_.asExpr))
'{
new A {
$exprs
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是编译器抱怨
A is not a class type
Run Code Online (Sandbox Code Playgroud) scala ×10
scala-3 ×10
scala-macros ×3
dotty ×2
akka ×1
akka-actor ×1
enums ×1
implicit ×1
macros ×1
polymorphism ×1
scalac ×1