3.5.3在某些情况下,scala使用了更一般的
一致性关系。弱S型
符合T型,书写S<:
W
T
,如果S<:t或S和T都是
基元数类型和s先于
按以下顺序排列。
-
字节& lt;
W
短
-
字节& lt;
W
性格
-
短小;
W
int
-
INT<
W
长
-
Long & lt;
W
浮标
-
浮动:
W
双重的
弱最小上界是
关于弱的最小上界
一致性。
这是在哪里用的?首先,它决定了
if
表达:
条件表达式的类型
是E2和E3类型的弱最小上界(_§3.5.3)
在scala 2.7.x中,按类型
AnyVal
,的最小上界
Int
和
Double
. 在2.8.x中,它键入
双重的
.
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
对于简单的数字加宽:
数值加宽。如果e有一个原语
弱符合的数字类型
(_§3.5.3)对于预期类型,它是
使用一个扩展到预期类型
的
6.26隐式转换97数字转换方法Toshort、Tochar,
Toint、Tolong、Tofloat和Todouble
_§12.2.1中定义。所需类型为
基元数字类型字节、短或
char,表达式e是
范围内的整型文字拟合
对于该类型,它将转换为
该类型中的文本相同。
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