PartialFunction orElse 的类型界限是否比应有的更宽松?

2024-02-11

让我们定义一个PartialFunction[String, String] and a PartialFunction[Any, String]

现在,给出定义orElse

def orElse[A1 <: A, B1 >: B](that: PartialFunction[A1, B1]): PartialFunction[A1, B1] 

我预计无法将两者组合起来,因为

AString
A1Any

因此有界A1 <: A (i.e. Any <: String)不成立。

没想到我竟然能把它们组合起来并获得PartialFunction[String, String]总体上定义String领域。这是一个例子:

val a: PartialFunction[String, String] = { case "someString" => "some other string" }
// a: PartialFunction[String,String] = <function1>

val b: PartialFunction[Any, String] = { case _ => "default" }
// b: PartialFunction[Any,String] = <function1>

val c = a orElse b
// c: PartialFunction[String,String] = <function1>

c("someString")
// res4: String = some other string

c("foo")
// res5: String = default

c(42)
// error: type mismatch;
//   found   : Int(42)
//   required: String

此外,如果我明确提供orElse类型参数

a orElse[Any, String] b
// error: type arguments [Any,String] do not conform to method orElse's type parameter bounds [A1 <: String,B1 >: String]

编译器终于显示出一些意义。

是否有我缺少的任何类型系统魔法导致b成为一个有效的论据orElse?换句话说,怎么会这样A1被推断为String?

如果编译器推断A1 from b那么它一定是Any,那么导致的推理链还会在哪里呢?String start?


Update

使用 REPL 后我注意到orElse返回交集类型A with A1当类型不匹配时。例子:

val a: PartialFunction[String, String] = { case "someString" => "some other string" }
// a: PartialFunction[String,String] = <function1>

val b: PartialFunction[Int, Int] = { case 42 => 32 }
// b: PartialFunction[Int,Int] = <function1>

a orElse b
// res0: PartialFunction[String with Int, Any] = <function1>

Since (String with Int) <:< String即使生成的函数实际上无法使用,这也是可行的。我也怀疑String with Any统一为Any, 鉴于

import reflect.runtime.universe._
// import reflect.runtime.universe._   

typeOf[String] <:< typeOf[String with Any]
// res1: Boolean = true

typeOf[String with Any] <:< typeOf[String]
// res2: Boolean = true

这就是为什么混合String and Any结果变成String.

话虽如此,幕后到底发生了什么?不匹配的类型是在什么逻辑下统一的?

Update 2

我已将问题简化为更一般的形式:

class Foo[-A] {
  def foo[B <: A](f: Foo[B]): Foo[B] = f
}

val a = new Foo[Any]
val b = new Foo[String]

a.foo(b) // Foo[String] Ok, String <:< Any
b.foo(a) // Foo[String] Shouldn't compile! Any <:!< String
b.foo[Any](a) // error: type arguments [Any] do not conform to method foo's type parameter bounds [A <: String]

你把这个搞颠倒了。

您始终可以传递给需要类型参数的方法A任何类型的参数B <: A,即任何子类型A。也就是说,如果你有

def foo(a: Animal)

你可以通过Dog to foo, 因为Dog <: Animal.

同样的,如果你有

def foo(l: List[Animal])

你可以通过List[Dog]对它来说,因为List与其类型参数协变,因为Dog <: Animal, then List[Dog] <: List[Animal]

现在如果你有

def foo(pf: PartialFunction[String, String])

你可以通过PartialFunction[Any, String], 因为PartialFunction与第一个类型参数逆变,与第二个类型参数协变。自从Any >: String, then PartialFuncion[Any, String] <: PartialFunction[String, String].

现在,对于类型界限,编译器将尝试推断A1 and B1,使得

  • A1是的子类型A
  • B2是的子类型B

为此,它将寻找:

  • 最常见的亚型Any and String, since A and A1处于逆变位置
  • 最不常见的超类型String and String, since B and B1是协变位置

Results

  • A1String
  • B1String

您编写的情况PartialFunction[String, String] 与一个PartialFunction[Int, Int]是上一个示例的一个奇怪的情况,其中:

  • 最常见的亚型String and Int is String with Int,即两种类型的交集,它是两者的子类型(在这种情况下几乎等于说Nothing: being both a String and an Int似乎不太可能)
  • 最不常见的超类型String and Int is Any

所以

val a: PartialFunction[String, String] = ...
val b: PartialFunction[Int, Int] = ...
a orElse b // PartialFunction[String with Int, Any] // as expected, although not very useful...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PartialFunction orElse 的类型界限是否比应有的更宽松? 的相关文章

随机推荐

  • 使用 Powershell 将 EBS 卷附加到 Windows EC2 [关闭]

    Closed 这个问题是与编程或软件开发无关 help closed questions 目前不接受答案 我看到很多关于在 Linux 上添加 EBS 卷的问题得到了解答 但在 Windows 上却没有 假设您发现磁盘空间不足 可能通过 C
  • 带通配符的 spring

    我想从多个位置加载键值对 我的第一个猜测是
  • 仅使用 JavaScript 的递归 HTML 表格树

    我开发了单击父节点来显示其子行 我只需要启用单击子数据即可将其子子行作为递归树或表树打开 有人可以添加你的逻辑来帮助我理解并帮助其他人吗 document getElementById products addEventListener c
  • 气流日志文件不存在:

    Airflow 在几周内工作正常 但突然开始出现几天错误 Dags 会因此错误而随机失败 日志文件不存在 airflow path 1 log获取自 http 8793 airflow path 1 log 无法从工作人员获取日志文件 对
  • Patentsview API Python 3.4

    我是Python的初学者 目前正在使用Python开发一个小项目 我想为 Patentsview org 的专利研究构建一个动态脚本 这是我的代码 import urllib parse import urllib request http
  • Android:带有链接链接的可点击 TextView

    我的布局中有一个 TextView 它在 XML 中设置了一个属性 clickable true 这是为了让它表现得像一个带有文本和图标的按钮 现在 我通过调用 HTML fromHtml 添加到 TextView HTML 文本 然后应用
  • 支持样式标签的 HTML Sanitizer for .NET

    我正在寻找一个好的 HTML 清理程序以在 ASP NET 项目中使用 问题是清理程序必须支持样式属性 这些属性可能包含 CSS 属性 这些属性也必须被清理 到目前为止我还没有找到一个好的产品可以使用 在我硬着头皮写自己的消毒剂之前 我想我
  • 在不能抛出的地方抛出NullPointerException

    我在一段无法抛出 NullPointerException 的代码中得到一个 NullPointerException 我开始认为在 JRE 中发现了一个错误 我使用javac 1 8 0 51作为编译器 问题出现在jre 1 8 0 45
  • 在 Electron process.stderr 中未定义

    我正在尝试在我的 Electron 应用程序中使用 sqlite3 nodejs 插件 我在加载 sqlite3 模块时遇到错误 查看崩溃的堆栈跟踪 我发现 sqlite3 有多个依赖项 其中之一是日志库 npmlog 该库尝试从以下位置初
  • Google Glass:执行“点击”事件或类似的事件

    请看下面的代码
  • 在 ES6 中如何调用我的类的父类的父类的构造函数?

    我正在使用 ES6 类 我的类 A 扩展了类 B 类 B 扩展了类 C A 如何扩展一个方法 然后调用该方法的 C 版本 class C constructor console log class c class B extends C c
  • 如何在 SQL Server Express 表中保存 QTime 并读取它?

    我在 SQL Server Express 中有一个包含字段的表name varchar 10 and timeVar time我想保存 a 的值QTime变量在time field 这是我尝试过的 QTime time QTime cur
  • 无法按名称激活工作表

    我试图在打开电子表格时根据当前月份激活每月工作表 问题出在脚本末尾 无法选择工作表 看起来getSheetByName gets a null值 不被接受setActiveSheet function selectmonth var now
  • 搜索大量大文本的最快方法

    我有一个用 C 编写的 Windows 应用程序 需要从数据库加载 250 000 行 并提供 键入时搜索 功能 这意味着只要用户在文本框中键入内容 应用程序就需要搜索所有 250 000 条记录 其中顺便说一句 单列 每行 1000 个字
  • 使用 Box2D 制作角色动画的最佳方法是什么?

    我正在为 Android 开发一款 2D 水下动作角色扮演游戏 使用 Box2D 作为物理引擎 主要用于碰撞检测 碰撞响应以及游戏角色在由墙壁 岩石和其他生物组成的环境中的移动 我尝试了两种不同的方法来使用 Box2D 实现角色动画 但都发
  • 无法将类型“System.Reflection.PropertyInfo”转换为

    我正在尝试使用反射来迭代我的类之一中的所有属性 public MDInstrument base PropertyInfo properties typeof MDInstrument GetProperties foreach Prope
  • Oauth2 PKCE 谁应该生成代码验证器和代码质询

    我正在编写将与某些 Oauth2 提供商集成的客户端应用程序 我将把原生 android 应用程序作为前端 将 spring boot 作为后端 我想将授权代码流与 PKCE 一起使用 我的问题是谁应该生成代码验证程序和代码挑战 我的后端还
  • 提取字符串的前(或后)n 个字符

    我想提取第一个 或最后一个 n字符串的字符 这相当于 Excel 的LEFT and RIGHT 一个小例子 create a string a lt paste left right sep a 1 leftright 我想生产b 一个等
  • Selenium 隐式等待与 Thread.sleep()

    根据 selenium 的说法 隐式等待会在一定时间内轮询 DOM 以查看元素是否出现 我的理解是 它将轮询指定的时间 但如果之前出现过某个元素 那么它将继续 而无需进一步等待 http seleniumhq org docs 04 web
  • PartialFunction orElse 的类型界限是否比应有的更宽松?

    让我们定义一个PartialFunction String String and a PartialFunction Any String 现在 给出定义orElse def orElse A1 lt A B1 gt B that Part