本文介绍了“弱一致性"是什么概念?在斯卡拉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近刚遇到弱一致性"这个词(在 Stack Overflow 用户 retronym 的回答中如何设置隐式转换以允许数字类型之间的算术运算?).

I just recently encountered the term "Weak Conformance" (in Stack Overflow user retronym's answer to How to set up implicit conversion to allow arithmetic between numeric types?).

这是什么?

推荐答案

  • 字节
  • 字节 <: 字符
  • 短 <: Int
  • Int
  • Float

    • Byte <: Short
    • Byte <: Character
    • Short <: Int
    • Int <: Long
    • Long <: Float
    • Float <: Double
    • 弱最小上限是弱的最小上限一致性.

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

      Where is this used? For one thing, it determines the type of if expressions:

      条件表达式的类型是 e2 和 e3 类型的弱最小上限(第 3.5.3 节)

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

      In Scala 2.7.x, this would by of type AnyVal, the least uppper bound of Int and Double. In 2.8.x, it types as 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>
      

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

      Another place it is used is in type inference:

      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
      

      也用于简单的数字加宽:

      And also for simple numeric widening:

      数字加宽.如果 e 有一个原语弱符合的数字类型(第 3.5.3 节)到预期的类型,它是使用一个扩展到预期类型的6.26 隐式转换 97 种数字转换方法 toShort、toChar、toInt, toLong, toFloat, toDouble在 §12.2.1 中定义.预期类型是原始数字类型 Byte、Short 或Char,表达式 e 是一个在范围内拟合的整数文字那种类型,它被转换为该类型中的相同文字.

      scala> 1: Double
      res10: Double = 1.0
      

      更新

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

      As pointed out by Daniel, the spec is wrong about which types have weak conformance. Let's ask the compiler itself:

      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
      

      这篇关于“弱一致性"是什么概念?在斯卡拉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 02:06