- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近刚刚遇到“弱一致性”这个术语(在 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.
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
类型,即 Int
和 Double
的最小上界。在 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/
我需要创建一个类型安全的弱引用数组 一个包含“类型安全”弱引用并且可以是数组元素的结构: public struct WeakRef: Hashable { public weak var r
假设我有这个 Swift 类: class Foo: NSObject, NSCoding { var stringMember: String var intMember: Int
谁能解释一下,下面的例子有什么问题?为什么会抛出 StackOverflowError 异常? (s/def ::tag keyword?) (s/def ::s string?) (s/def ::
啊啊!尝试使用我的神经网络进行计算时,我不断收到以下错误: > net.compute matrix.train1 (Intercept) survived pclass sexmale
我在下面的行中面临警告 此警告在更改以下行后出现 @interface BDSAppDelegate : UIResponder 到 @interface BDSAppDelegate : UIRe
我正在尝试实现 git 项目 XLPagerTabStrip . 根据项目,每个 Controller 必须: Every view controller provided by PagerTabSt
clojure.spec.alpha API 有一个名为 conformer 的宏有这样的描述: Usage: (conformer f) (conformer f unf) takes
iPhone开发中的“符合”是什么意思?这个词被广泛使用,但我找不到令人满意的定义。 假设我们有 3 个类别 - A、B 和 C 如果A继承B&如果A符合C 这是什么意思? 另外,为什么大多数类、协议
我最近刚刚遇到“弱一致性”这个术语(在 Stack Overflow 用户 retronym 对 How to set up implicit conversion to allow arithmet
我在我的一个 MVC 项目中使用 ConfORM Nhibernate。并且有一对多映射的问题。 IEnumerable domainEntities = this.GetDomain
如何对两个数组进行逐元素算术运算在第一个维度上是一致的,但还有一个额外的维度? 示例,将数组 a (3 x 3 x 2) 乘以数组 b (3 x 3): a a * b 要使其工作,您必须将数组 b
我是 swift 新手,并按照教程了解更多信息。 在下面的代码中,我定义了自定义运算符( Bool { for cardValue in valueArray { if lhs
我有来自 here 的 FAA 剖面图.每个包都包含相关图表的 .tif、描述图表的 .htm 文件和 .tfw 世界文件。 map 投影为朗伯共形圆锥投影。 我正在开发一个 C# 应用程序,它需要能
当我在脚本中调用下面的函数时,我会看到一个弹出对话框来执行一些说明。 这是我的示例代码: function updateStatus(instrxnID){ exporter.
我试图通过注入(inject)适应 URLSession 和 URLSessionDataTask 协议(protocol)的对象来测试我自己的类。我正在扩展 NSURLSession 和 NSURL
这个问题在这里已经有了答案: Fatal error: Dictionary does not conform to Decodable because Any does not conform t
我想在我的一个代理类中管理所有位置代码。由于该类(class)是从头开始构建的,而不是构建在 UIView 上的或继承 NSObjectProtocol 的类似类,它会抛出错误“不符合协议(proto
无法理解为什么我的类不符合 Codable请注意,在我的例子中,我不需要实现方法encode 和decode。 public class LCLAdvantagePlusJackpotCache: C
我是 IOS swift 开发新手。我曾经使用以前的 Xcode 6 beta。 我已经下载了 Xcode 6.0.1 但我无法让它工作 Xcode Version: 6.0.1 当我尝试运行示例时,
以下 Scala 声明是可以的: trait Base[B y) then x else y 这相当于将 Ordering(与 java.util.Comparator 相同)传递给函数。确实,宣言
我是一名优秀的程序员,十分优秀!