标签: dotty

Dotty 如何决定如何推断/何时扩大联合类型?

此处讨论了扩大联合类型但我似乎无法找到以下案例的答案

让我们从以下内容开始

val x = List(1, 2, "a")
Run Code Online (Sandbox Code Playgroud)

这个异构列表被推断为List[Any]就像在 Scala 2 中一样

然而以下

val x2 = List(List(1, 2), Vector("a", "b"))
Run Code Online (Sandbox Code Playgroud)

推断为 List[scala.collection.immutable.AbstractSeq[Int | String]]

这是相当混乱的行为。为什么Any在一种情况下会推断出两种不相交类型的 LUB ,而在另一种情况下会推断出联合类型?

如果这只是一个设计决定,是否有任何此类情况需要人们注意?

scala type-inference dotty union-types scala-3

7
推荐指数
1
解决办法
167
查看次数

如何在Scala中完全避免运行时反射?

请注意,这是一个社区帖子,应根据需要添加示例.如果您无法直接编辑添加示例(问题示例或解决方案)的答案,请在评论中发布带有要点(或类似内容)的链接或添加单独的答案,以便稍后进行集成.

Scala 3可能根本不包含scala.reflect.runtime(Dotty目前没有,并且计划这样做不确定).虽然适用于Scala 2和Dotty的答案可能是转换目的和立即改进性能的首选,但也欢迎Dotty特定的解决方案.

参考

https://www.cakesolutions.net/teamblogs/ways-to-pattern-match-generic-types-in-scala

scala dotty

6
推荐指数
1
解决办法
445
查看次数

如何使用宏在 Dotty 中生成一个类?

是否可以在 Dotty, Scala 3 中生成一个带有宏的新类?

兹拉亚

annotations scala metaprogramming dotty scala-macros

6
推荐指数
2
解决办法
729
查看次数

如何在 dotty 中解压元组中的元素类型?

假设我有一个类定义为:

case class Box[A](a: A)
Run Code Online (Sandbox Code Playgroud)

我想编写一个通用方法,(Box[A1](a1), .., Box[An](an))将元组解包为(a1, .., an)类型为 的元组(A1, .., An)

我试过Match Types没有运气:

scala> type Unpack[Bs <: Tuple] <: Tuple = Bs match {
     |   case Unit => Unit
     |   case Box[a] *: bs => a *: Unpack[bs]
     | }

scala> def unpack[Bs <: Tuple](bs: Bs): Unpack[Bs] = bs match {
     |   case () => ()
     |   case Box(a) *: as => a *: unpack(as)
     | }
2 |  case …
Run Code Online (Sandbox Code Playgroud)

scala dotty scala-3

6
推荐指数
1
解决办法
407
查看次数

有没有办法使用 Gradle 的 Scala 3 编译器(Dotty)?

我是 Gradle 和 Dotty 的新手(而且对 Scala 整体来说仍然相对较新)。我能够像这样用 Gradle 创建一个 Scala 项目:

gradle init --dsl kotlin --type scala-library --package com.stackoverflow.example
Run Code Online (Sandbox Code Playgroud)

而且我知道 Dotty 可以与 SBT 一起使用。但是有没有办法将 Dotty 与 Gradle 连接起来(还)?

scala gradle dotty gradle-scala scala-3

6
推荐指数
1
解决办法
907
查看次数

2 不同类中具有相同名称的扩展方法在 Scala 3 中不起作用?

我有以下场景:

case class B(v: String)
case class A(bs: Seq[B])

extension(a: A)
  def doit() = a.bs.map(_.doit()) // here is the exception

extension(b: B)
  def doit() = println("OK: ${b.v}")
Run Code Online (Sandbox Code Playgroud)

这给了我以下编译异常:

value doit is not a member of B.
An extension method was tried, but could not be fully constructed:

    _$1
Run Code Online (Sandbox Code Playgroud)

Scala 3中扩展方法的命名有限制吗?

请参阅Scastie上的示例

extension-methods scala dotty scala-3

6
推荐指数
1
解决办法
726
查看次数

Dotty 提供什么来替换字体投影?

我一直在阅读有关 Dotty 的文章,因为它看起来即将成为 Scala 3,并注意到类型投影被认为是“不健全的”并从语言中删除......

这似乎很糟糕,因为我已经看到了几个非常有用的用例。例如:

trait Contents
class Foo extends Contents
class Bar extends Contents

trait Container[T <: Contents] { type ContentType = T }
class FooContainer extends Container[Foo]
class BarContainer extends Container[Bar]

trait Manager[T <: Container[_]] { 
  type ContainerType = T 
  type ContentType = T#ContentType
  def getContents: ContentType 
  def createContainer(contents: ContentType): ContainerType
}
Run Code Online (Sandbox Code Playgroud)

如何在 Dotty 中做这样的事情?将第二个类型参数添加到Manager? 但是,除了创建和操作 的实例变得非常乏味这一事实之外Manager,它也不太有效,因为没有办法强制执行这两种类型之间的关系(Manager[FooContainer, Bar]不应该是合法的)。

然后,还有其他用途,如类型 lambdas 和部分应用类型,可用于创建有偏差的函子等……或者这些(部分应用类型)是否成为 Dotty 中的“一等公民”?

编辑

为了回答评论中的问题,这里有一个有点人为的例子,可以使用他的 this 。让我们假设,我Managers实际上是 Akka Actors …

scala dotty type-projection scala-3

5
推荐指数
1
解决办法
445
查看次数

如何从 Visual Studio Code UI 执行 dotty 代码

我正在尝试使用Visual Studio Code来运行 dotty 代码。我开始使用IDEsbt launchIDE根据来自指令这个页面,我也安装Dotty Language ServerCode Runner扩展。Dotty 是使用安装的brew,我可以从 CMD 编译和执行 dotty 代码。

问题是我无法运行此代码,Visual Studio Code因为我Code Runner试图使用 scala 而不是 dotty 来执行它。

找不到任何有用的配置来调整此插件以使用 dotty。

有什么方法可以让它从 Visual Studio Code UI 工作吗?

scala dotty visual-studio-code scala-3

5
推荐指数
1
解决办法
672
查看次数

如何确保元组是同质的?

由于我无法控制的原因,我的方法以元组的形式接收输入。这个元组应该只包含 的实例Foo,即它应该看起来像(Foo, Foo ... Foo)并且不应该有StringInt在里面。我想在编译时检查这个而不是在运行时抛出异常。我怎样才能做到这一点?

以下是我目前拥有的代码,这是不对的:

def f(tupleOfFoos: Tuple): Tuple = {
  for (x <- tupleOfFoos) assert(x.isInstanceOf[Foo])
  mapTuple(tupleOfFoos, irrelevantFunction)
}
Run Code Online (Sandbox Code Playgroud)

我愿意使用 Shapeless 或 Dotty/Scala 3 中引入的新功能。

scala tuples dotty scala-3

5
推荐指数
1
解决办法
120
查看次数

如何在 Scala 3 / Dotty 中实现像 MapK 这样的类型?

我正在尝试但未能让这样的东西在 Scala 3 中工作:

type TupleK[K[*], V[*], A] = (K[A], V[A]) 

final class MapK[K[*], V[*]] private (val rawMap: Map[K[?], V[?]]) {
    
  def foreach(f: TupleK[K, V, ?] => Unit): Unit = {
    rawMap.foreach(f.asInstanceOf[Tuple2[K[?], V[?]] => Any])
  }
}

object MapK {
  
  def apply[K[*], V[*]](entries: TupleK[K, V, ?]*): MapK[K, V] = {
    new MapK[K, V](Map(entries: _*))
  }
}
Run Code Online (Sandbox Code Playgroud)

像这样使用:

class Key[A]()
  
type Id[A] = A
  
val intKey = Key[Int]
val strKey = Key[String]

MapK[Key, Id](intKey -> 1, strKey -> "a")
Run Code Online (Sandbox Code Playgroud)

在 Scala …

scala existential-type dotty higher-kinded-types scala-3

5
推荐指数
1
解决办法
177
查看次数