gpt4 book ai didi

scala - Scala中 "weak conformance"是什么概念?

转载 作者:行者123 更新时间:2023-12-03 00:43:55 25 4
gpt4 key购买 nike

我最近刚刚遇到“弱一致性”这个术语(在 Stack Overflow 用户 retronym How to set up implicit conversion to allow arithmetic between numeric types? 的回答中)。

这是什么?

最佳答案

3.5.3 Weak Conformance In some situations Scala uses a more general conformance relation. A type S weakly conforms to a type T , written S <:w T , if S <: T or both S and T are primitive number types and S precedes T in the following ordering.

  • 字节 <:w
  • 字节<:w字符
  • 短<:w 整数
  • 整型 <:w 长整型
  • 长<:w浮点
  • float <:w double

A weak least upper bound is a least upper bound with respect to weak conformance.

这个用在什么地方?一方面,它确定 if 表达式的类型:

The type of the conditional expression is the weak least upper bound (§3.5.3) of the types of e2 and e3

在 Scala 2.7.x 中,这将是 AnyVal 类型,即 IntDouble 的最小上界。在 2.8.x 中,它的类型为 Double

scala> if (true) 1 else 1d
res0: Double = 1.0

同样:

scala> try { 1 } catch { case _ => 1.0 }
res2: Double = 1.0

scala> (new {}: Any) match { case 1 => 1; case _ => 1.0 }
res6: Double = 1.0

scala> def pf[R](pf: PartialFunction[Any, R]): PartialFunction[Any, R] = pf
pf: [R](pf: PartialFunction[Any,R])PartialFunction[Any,R]

scala> pf { case 1 => 1; case _ => 1d }
res4: PartialFunction[Any,Double] = <function1>

它的另一个用途是类型推断:

scala> def foo[A](a1: A, a2: A): A = a1
foo: [A](a1: A,a2: A)A

scala> foo(1, 1d)
res8: Double = 1.0

scala> def foos[A](as: A*): A = as.head
foos: [A](as: A*)A

scala> foos(1, 1d)
res9: Double = 1.0

也可以用于简单的数字加宽:

Numeric Widening. If e has a primitive number type which weakly conforms (§3.5.3) to the expected type, it is widened to the expected type using one of the 6.26 Implicit Conversions 97 numeric conversion methods toShort, toChar, toInt, toLong, toFloat, toDouble defined in §12.2.1. expected type is a primitive numeric type Byte, Short or Char, and the expression e is an integer literal fitting in the range of that type, it is converted to the same literal in that type.

scala> 1: Double
res10: Double = 1.0

更新

正如 Daniel 所指出的,规范关于哪些类型的一致性较弱的问题是错误的。让我们问问编译器本身:

scala> :power
** Power User mode enabled - BEEP BOOP **
** scala.tools.nsc._ has been imported **
** New vals! Try repl, global, power **
** New cmds! :help to discover them **
** New defs! Type power.<tab> to reveal **

scala> settings.maxPrintString = 10000


scala> import global.definitions._
import global.definitions._

scala> (for{c1 <- ScalaValueClasses;
c2 <- ScalaValueClasses
isNSC = isNumericSubClass(c1, c2)
if isNSC
} yield ("isNumericSubClass (%s, %s) = %b" format (c1, c2, isNSC))).mkString("\n")


res5: String =
isNumericSubClass (class Byte, class Byte) = true
isNumericSubClass (class Byte, class Short) = true
isNumericSubClass (class Byte, class Int) = true
isNumericSubClass (class Byte, class Long) = true
isNumericSubClass (class Byte, class Float) = true
isNumericSubClass (class Byte, class Double) = true
isNumericSubClass (class Short, class Short) = true
isNumericSubClass (class Short, class Int) = true
isNumericSubClass (class Short, class Long) = true
isNumericSubClass (class Short, class Float) = true
isNumericSubClass (class Short, class Double) = true
isNumericSubClass (class Int, class Int) = true
isNumericSubClass (class Int, class Long) = true
isNumericSubClass (class Int, class Float) = true
isNumericSubClass (class Int, class Double) = true
isNumericSubClass (class Long, class Long) = true
isNumericSubClass (class Long, class Float) = true
isNumericSubClass (class Long, class Double) = true
isNumericSubClass (class Char, class Int) = true
isNumericSubClass (class Char, class Long) = true
isNumericSubClass (class Char, class Char) = true
isNumericSubClass (class Char, class Float) = true
isNumericSubClass (class Char, class Double) = true
isNumericSubClass (class Float, class Float) = true
isNumericSubClass (class Float, class Double) = true
isNumericSubClass (class Double, class Double) = true

关于scala - Scala中 "weak conformance"是什么概念?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3094380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com