gpt4 book ai didi

scala - 类型注释被推断的表达式类型覆盖

转载 作者:行者123 更新时间:2023-12-03 23:26:32 25 4
gpt4 key购买 nike

在 scala 编程语言中,鉴于我将用更广泛的类型注释表达式并提供一个狭窄的值,我的程序被拒绝:

scala> def x[A](): A = 8 
<console>:11: error: type mismatch;
found : Int(8)
required: A
def x[A]() = 8: A
而在 ocaml 中,当我做同样的事情时,程序被接受,但表达式的类型注释被更窄的表达式的类型覆盖。
utop # let x (): 'a = 8 ;;
val x : unit -> int = <fun>
这些类型系统之间的区别是什么,导致的情况是,在一种情况下程序被拒绝,而在另一种情况下它被接受?

最佳答案

@craigfe 实际上只是写了 a very accessible post关于这个昨天,我强烈建议阅读。
但简短的回答是 OCaml 中注释中的类型变量是统一变量,而不是多态类型约束。它们表示编译器要推断的未知类型,编译器更喜欢更通用的解决方案,并可能推断它是多态的,但如果不可能,则将其推断为特定类型。
为了获得您期望的行为,您必须明确指出类型变量应该使用 'a. 进行普遍量化。 ,通常读作“for all a's”:

utop # let x: 'a. unit -> 'a = fun () -> 8 ;;
^^^^^^^^^^^
Error: This definition has type unit -> int which is less general than
'a. unit -> 'a

关于scala - 类型注释被推断的表达式类型覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64220935/

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