Scala 3中类型推断的变化

Scala 3中类型推断的变化,scala,type-inference,dotty,scala-3,Scala,Type Inference,Dotty,Scala 3,Scala 3将在类型推断方面带来什么变化?目前的文件只是说明。比如说, Scala 2.13 scala> val i: Int = 42 val i: Int = 42 scala> val c: Char = 'a' val c: Char = a scala> List(i,c) val res0: List[Int] = List(42, 97) scala> 42 == Some(42) ^ warning: compa

Scala 3将在类型推断方面带来什么变化?目前的文件只是说明。比如说,

Scala 2.13

scala> val i: Int = 42
val i: Int = 42

scala> val c: Char = 'a'
val c: Char = a

scala> List(i,c)
val res0: List[Int] = List(42, 97)
scala> 42 == Some(42)
          ^
       warning: comparing values of types Int and Some[Int] using `==` will always yield false
val res2: Boolean = false
scala> def f[T](i: T, g: T => T) = g(i)
def f[T](i: T, g: T => T): T

scala> f(41, x => x + 1)
             ^
       error: missing parameter type
scala> def f[F <: List[A], A](as: F) = as
def f[F <: List[A], A](as: F): F

scala> f(List(42))
       ^
       error: inferred type arguments [List[Int],Nothing] do not conform to method f's type parameter bounds [F <: List[A],A]
             ^
       error: type mismatch;
        found   : List[Int]
        required: F

Scala 3(dotty 0.24.0-RC1)

平等 Scala 2.13

scala> val i: Int = 42
val i: Int = 42

scala> val c: Char = 'a'
val c: Char = a

scala> List(i,c)
val res0: List[Int] = List(42, 97)
scala> 42 == Some(42)
          ^
       warning: comparing values of types Int and Some[Int] using `==` will always yield false
val res2: Boolean = false
scala> def f[T](i: T, g: T => T) = g(i)
def f[T](i: T, g: T => T): T

scala> f(41, x => x + 1)
             ^
       error: missing parameter type
scala> def f[F <: List[A], A](as: F) = as
def f[F <: List[A], A](as: F): F

scala> f(List(42))
       ^
       error: inferred type arguments [List[Int],Nothing] do not conform to method f's type parameter bounds [F <: List[A],A]
             ^
       error: type mismatch;
        found   : List[Int]
        required: F

斯卡拉3

scala> 42 == Some(42)
1 |42 == Some(42)
  |^^^^^^^^^^^^^^
  |Values of types Int and Some[Int] cannot be compared with == or !=
scala> def f[T](i: T, g: T => T) = g(i)
def f[T](i: T, g: T => T): T

scala> f(41, x => x + 1)
val res0: Int = 42
scala> def f[F <: List[A], A](as: F) = as                                                                                                                
def f[F <: List[A], A](as: F): F

scala> f(List(42))                                                                                                                                       
val res0: List[Int] = List(42)

至于你的
等式
例子,它实际上是由新的引起的,这几乎意味着如果你有一个
Eql[A,B]
,其中A是B,那么类型A只能与它有一个
Eql
实例的事物相比较(形式为
Eql[A,C]
Eql[C,A]

就scala 3的一般类型推断而言,主要有:

  • :我们现在可以表示联合类型和表达式,如

    if(条件)1 else“1”
    
    应为推断为类型
    Int | String

  • :联合类型的一个新用途是描述可为null的类型,例如,我们可以用Java编写这样的代码:

    公共字符串getUser(int-id){
    如果(id==0){
    返回“Admin”;
    }
    返回null;
    }
    
    在scala 2中,我们还可以写:

    def getUser(id:Int):String=if(id==0)返回“Admin”或null
    
    但在scala 3中,这种方法也必须声明为
    String | Null
    类型,以表示其可空性,并且在较新版本的scala 3中默认情况下不会编译

    当使用Java时,它变得更加复杂,因此如果您想了解更多,我建议您阅读链接中的内容

  • GADT:类似于
    @functioninterface
    在java中的工作方式,我们知道有GADT。 这意味着如果你有一个未实现的方法的特点:

    trait可食{
    def foo():单位
    }
    
    您可以通过传递带有该签名的lambda来创建它的实例,因此在本例中:

    val fooable:fooable=()=>打印(“Fooing”)
    
    还有一些,包括,但这些是主要的


推断类型现在通过单参数列表的其余部分传播,这意味着可能不需要使用多个参数列表来辅助推断

Scala 2.13

scala> val i: Int = 42
val i: Int = 42

scala> val c: Char = 'a'
val c: Char = a

scala> List(i,c)
val res0: List[Int] = List(42, 97)
scala> 42 == Some(42)
          ^
       warning: comparing values of types Int and Some[Int] using `==` will always yield false
val res2: Boolean = false
scala> def f[T](i: T, g: T => T) = g(i)
def f[T](i: T, g: T => T): T

scala> f(41, x => x + 1)
             ^
       error: missing parameter type
scala> def f[F <: List[A], A](as: F) = as
def f[F <: List[A], A](as: F): F

scala> f(List(42))
       ^
       error: inferred type arguments [List[Int],Nothing] do not conform to method f's type parameter bounds [F <: List[A],A]
             ^
       error: type mismatch;
        found   : List[Int]
        required: F

斯卡拉3

scala> 42 == Some(42)
1 |42 == Some(42)
  |^^^^^^^^^^^^^^
  |Values of types Int and Some[Int] cannot be compared with == or !=
scala> def f[T](i: T, g: T => T) = g(i)
def f[T](i: T, g: T => T): T

scala> f(41, x => x + 1)
val res0: Int = 42
scala> def f[F <: List[A], A](as: F) = as                                                                                                                
def f[F <: List[A], A](as: F): F

scala> f(List(42))                                                                                                                                       
val res0: List[Int] = List(42)
我想这一变化可能与


当类型参数不以术语形式出现时,可以进行更好的推断

Scala 2.13

scala> val i: Int = 42
val i: Int = 42

scala> val c: Char = 'a'
val c: Char = a

scala> List(i,c)
val res0: List[Int] = List(42, 97)
scala> 42 == Some(42)
          ^
       warning: comparing values of types Int and Some[Int] using `==` will always yield false
val res2: Boolean = false
scala> def f[T](i: T, g: T => T) = g(i)
def f[T](i: T, g: T => T): T

scala> f(41, x => x + 1)
             ^
       error: missing parameter type
scala> def f[F <: List[A], A](as: F) = as
def f[F <: List[A], A](as: F): F

scala> f(List(42))
       ^
       error: inferred type arguments [List[Int],Nothing] do not conform to method f's type parameter bounds [F <: List[A],A]
             ^
       error: type mismatch;
        found   : List[Int]
        required: F


scala>def[F AFAIK,一般来说是改进和联合类型。不确定这是否符合推断条件,但现在可以在同一参数列表中引用路径依赖类型,因此不需要辅助模式。@MarioGalic您可以观看Guillaume Martres的演讲幻灯片:关于GADT bulletpoint-
Fooable
似乎是SAM,它一直是av有一段时间不可用。SAM与GADT的关系如何?对不起,你是对的,我是说SAM,抱歉混淆了名称,我实际上是无意中合并了两点。关于GADT,我想说的是GADT成员(特别是枚举成员)在实例化时被推断为枚举的类型,而不是它们的实际类型
将Bool.True推断为
Bool
类型,而不是Bool.True,即使这是它的底层类型。关于SAMs,事实是它们现在是为java类型推断的(不再是
new Runnable(){def run()..}