我无法使用 Scala 3 在 ScalaDoc 中添加包描述
/** package description */
package foo {
/** some scaladoc comment */
def method():A = ???
}
Run Code Online (Sandbox Code Playgroud)
然后我用 sbt (1.5.3) 生成文档
sbt> doc
Run Code Online (Sandbox Code Playgroud)
我没有在文件中获得包本身的描述,而只获得包内部index.html
的描述。method
如何TypeRepr
在更高级的类型上正确地进行模式匹配?存在成功匹配类,但是当我尝试使用类型时,出现编译器错误unreducible application of higher-kinded type writetype.Foo to wildcard arguments
import scala.quoted.*
type Foo[X]
class Bar[X]
inline def writeType[T]: String = ${writeTypeImpl[T]}
def writeTypeImpl[T](using Type[T], Quotes): Expr[String] =
import quotes.reflect.*
val tpe = TypeRepr.of[T]
val s = tpe.asType match
//case '[Foo[?]] => "FooSuccess"
case '[Bar[?]] => "BarSuccess"
case _ => "Fail"
Expr(s)
Run Code Online (Sandbox Code Playgroud) 我正在学习 Scala 3,我对匹配类型和文字类型很感兴趣。
我想编写一个函数,它采用几种文字类型之一,并返回特定类型作为传入文字类型的函数。
这是我正在尝试做的一个相当简单的例子:
type TestMatchType[T] = T match
case "String1" => Int
case "String2" => String
def testMatchType[T](input: T): TestMatchType[T] =
input match
case "String1": "String1" => 1
case "String2": "String2" => "Two"
val string1Output: Int = testMatchType("String1")
Run Code Online (Sandbox Code Playgroud)
用言语来说:
testMatchType
它接受一个参数,该参数的类型要么是文字类型"String1"
,要么是文字类型"String2"
。如果传递了具有类型的参数"String1"
,则该函数应该返回一个Int
。如果传递了具有类型的参数"String2"
,该函数应该返回一个String
。如果传递的参数的类型不是这些类型文字之一,则会产生编译时错误。但是,当我尝试编译上面的代码时,出现以下错误:
1 |val string1Output: Int = testMatchType("String1")
| ^^^^^^^^^^^^^^^^^^^^^^^^
| Found: TestMatchType[String]
| Required: Int
|
| Note: a match type could not be …
Run Code Online (Sandbox Code Playgroud) 我试图理解 Scala3 新的“多元平等”功能。在比较不同类型时,我遇到了不一致的行为。
情况 1. 比较 Int 和 String:
val x = 1
val y = "One"
x == y // gives compilation error -> "Values of types Int and String cannot be compared with == or !="
Run Code Online (Sandbox Code Playgroud)
scala.language.strictEquality
案例 2. 比较两个案例类:
case class Cat(catname: String)
case class Dog(dogname: String)
val d = Dog("Frank")
val c = Cat("Morris")
d == c // false, but it compiles
Run Code Online (Sandbox Code Playgroud)
我知道我需要import scala.language.strictEquality
在 case2 中强制执行多重平等。但为什么 case1 不需要呢?
我有2个填充Array[Int]的实现,如下所述。第一个执行时间为 84 毫秒,第二个执行速度慢 100 倍:
Execute 'filling via Array.fill' in 84 ms
Execute 'filling via while' in 8334 ms
Run Code Online (Sandbox Code Playgroud)
为什么第二个变体需要 100 倍的时间?它不是 GC,因为我可以在第二次执行时以相同的时间删除第一次执行。我在 Java 11 上运行它,并使用 Scala 3:
.jdks/adopt-openjdk-11.0.11/bin/java ... Fill
Run Code Online (Sandbox Code Playgroud)
更重要的是,如果您打开Array.fill
实现,您将通过 while... 看到实现
Execute 'filling via Array.fill' in 84 ms
Execute 'filling via while' in 8334 ms
Run Code Online (Sandbox Code Playgroud)
PS:我在 Scala 2.12 上重复这个测试:
Execute 'filling via Array.fill' in 118 ms
Execute 'filling via while' in 6 ms
Run Code Online (Sandbox Code Playgroud)
Scala 3 中的问题...
PPS:在这种情况下,for (i <- 0 until n)
可以正常速度工作,计时与 Scala …
我有一个不透明类型,它是通过在伴生对象范围中FancyDouble
使用从 Double 隐式转换的。given Conversion[Double, FancyDouble] = FancyDouble(_)
之后,构造val d: FancyDouble = 0.0
工作,但是当我尝试进行类似的比较时if (d == 0.0)
,编译器抱怨我无法比较FanceDouble
and Double
(我期望它应该将0.0
文字隐式转换为 FancyDouble,就像前面的情况一样。
如何启用与隐式转换的比较?
我的问题很简单。我有以下内容:
enum Colors:
case Blue, Red, Green
Run Code Online (Sandbox Code Playgroud)
我如何从这个枚举中选择一个随机元素?我尝试了这个问题的解决方案,但没有成功。
目前正在学习 Scala 3 隐式,但我很难理解 \xe2\x80\x8bas
和with
关键字在如下定义中的作用:
given listOrdering[A](using ord: Ordering[A]) as Ordering[List[A]] with\n \xe2\x80\x8bdef compare(a: List[A], b: List[A]) = ...\n
Run Code Online (Sandbox Code Playgroud)\n我尝试谷歌搜索,但没有找到任何好的解释。我已经检查了 Scala 3 参考指南,但我发现的唯一一件事as
是它是一个“软修饰符”,但这并不能真正帮助我理解它的作用......我猜as
在上面的代码以某种方式用于澄清这listOrdering[A]
是一个Ordering[List[A]]
(就像正在进行某种类型的打字或类型转换?),但如果能找到它背后的真正含义那就太好了。
至于with
,我只在 Scala 2 中使用它来继承多个特征(class A extends B with C with D
),但在上面的代码中,它似乎以不同的方式使用......
非常感谢任何解释或为我指明查看文档的正确方向!
\n另外,如果用 Scala 2 编写上面的代码会是什么样子?也许这可以帮助我弄清楚发生了什么事......
\nScala 3 中有没有办法将derives
关键字与不透明类型别名结合使用?最好有一种无样板的方法,通过自动依赖基础类型(如果有)的相同类型类的实例来为给定的不透明类型别名提供类型类实例。
如果能够表达类似的东西就好了
opaque type Id = Int
object Id:
given Show[Id] = Show.intShow
Run Code Online (Sandbox Code Playgroud)
对于某些假设的类型类Show
,如
opaque type Id = Int derives Show
Run Code Online (Sandbox Code Playgroud) 假设我有一些类型类
trait FooBar[X]
Run Code Online (Sandbox Code Playgroud)
和一个实例FooBar[Int]
:
given intIsFooBar: FooBar[Int] = new FooBar {}
Run Code Online (Sandbox Code Playgroud)
现在,假设我有一个Intf
具有某种成员类型的接口A
,并且还保证有一个given FooBar[A]
:
trait Intf:
type A
given aIsFoobar: FooBar[A]
Run Code Online (Sandbox Code Playgroud)
现在,我有了类型Int
,也有一个FooBar[Int]
,但是我如何实际实现这个接口呢Int
?
如果我尝试
class IntImpl() extends Intf:
type A = Int
given aIsFoobar: FooBar[A] = summon
Run Code Online (Sandbox Code Playgroud)
然后我收到“函数体 IntImpl.aIsFoobar 中的无限循环”错误,因为summon
似乎看到的是aIsFoobar
而不是intIsFooBar
。
如果我尝试使用summon
某些辅助辅助变量中的实例,如下所示:
class IntImpl() extends Intf:
type A = Int
private final val _aIsFoobar: FooBar[A] = summon
given aIsFoobar: …
Run Code Online (Sandbox Code Playgroud) scala ×10
scala-3 ×10
given ×2
implicit ×2
arrays ×1
deriving ×1
derivingvia ×1
enums ×1
match-types ×1
optimization ×1
random ×1
scala-macros ×1
scaladoc ×1
syntax ×1
typeclass ×1