- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否应该在不需要显式类型定义的情况下编译以下 this
?
def prepList[B >: A](prefix: PlayList[B]) : PlayList[B] =
prefix.foldr(this: PlayList[B])((node, suffix) => suffix.prepNode(node))
B >: A
根据定义 this
有类型 PlayList[A]
,它是 PlayList[B]
的子类型自 B >: A
并且播放列表在 A
中是协变的. node
有类型 B
,参数类型prefix
. f
在 foldr
与 B
的第一个参数具有相同的类型(声明为 foldr
) . suffix
与 this
的类型相同,所以特别是一个 PlayList[A]
.自 B >: A
, suffix.prepNode()
需要一个 B
. suffix.prepNode(node)
哪里是合法的
node
有类型
B
.只有当我在调用
foldr
时明确指定类型时,它似乎才能做到这一点。或引用
this
在那个调用中。
(node: B, suffix: PlayList[B])
,方法调用的参数仍然会产生类型不匹配错误
suffix.prepNode(node)
:
"found: B, required: A"
sealed abstract class PlayList[+A] {
import PlayList._
def foldr[B](b: B)(f: (A, B) => B): B
def prepNode[B >: A](b: B): PlayList[B] = nel(b, this)
def prepList[B >: A](prefix: PlayList[B]): PlayList[B] =
// need to specify type here explicitly
prefix.foldr(this: PlayList[B])((node, suffix) => suffix.prepNode(node))
override def toString = foldr("")((node, string) => node + "::" + string)
}
object PlayList {
def nil[A]: PlayList[A] = Nil
def nel[A](head: A, tail: PlayList[A]): PlayList[A] = Nel(head, tail)
def nel[A](as: A*): PlayList[A] = as.foldRight(nil[A])((a, l) => l.prepNode(a))
}
case object Nil extends PlayList[Nothing] {
def foldr[B](b: B)(f: (Nothing, B) => B) = b
}
case class Nel[+A](head: A, tail: PlayList[A]) extends PlayList[A] {
def foldr[B](b: B)(f: (A, B) => B) = f(head, tail.foldr(b)(f))
}
foldr
接受类型参数 (T)((U, T) => T)
.我们试图推断类型的值 U
和 T
. foldr
有关系和函数的第二个参数——它们是一样的,T
. (对丹尼尔的部分回答。)this: PlayList[A]
和 suffix: PlayList[B]
B >: A
,最具体的常见父类(super class)型是PlayList[B]
;因此我们有 T == PlayList[B]
. 备注 我们不需要 U
之间的任何关系和 T
推断出这一点。 node
有类型 B
(即 U == B
)。 U == B
的结论的。不从 suffix
的类型参数推断它. (Scala 编译器能做到这一点吗?)U == B
,我们就编译成功了。那么哪一步出错了呢? foldr
上面的参数类型我错过了
U == A
根据定义,它是
PlayList
的类型参数类(class)。我认为这仍然与上述步骤一致,因为我们在
PlayList[B]
的实例上调用它。 .
T == PlayList[B]
作为一些东西中最不常见的父类(super class)型,以及
U == B
根据
foldr
的定义在接收器上。这似乎足够简洁,可以缩小到几个选项:
B
的上限PlayList[B]
的 foldr
到 prepNode
的参数类型(怀疑)最佳答案
我不是类型专家,但这是我尝试推断时发生的情况。((node, suffix) => suffix.prepNode(node))
返回一些未知类型 PlayList[T]
,其中 T 扩展 A 。它作为参数传递给 foldr,foldr 返回传递给它的函数的类型(PlayList[T]
,其中 T 扩展 A)。这应该是某种类型 PlayList[B]
.
所以我的猜测是 this:PlayList[B]
有必要表明 T 和 B 是相关的。
可能您需要将 PlayList 设置为两种类型的参数 PlayList[+A, B >: A]
因为您的 prepNode 和 propList 似乎适用于扩展 A 的相同类型?
换句话说,您的原始类定义可能是这样定义的:
def prepNode[T >: A](b: T): PlayList[T]
def prepList[U >: A](prefix: PlayList[U]): PlayList[U]
:PlayList[B]
(使用 2.8.0.RC1):
$ scalac -explaintypes -d classes Infer.scala
found : node.type (with underlying type B)
required: A
prefix.foldr(this)((node, suffix) => suffix.prepNode(node))
^
node.type <: A?
node.type <: Nothing?
B <: Nothing?
<notype> <: Nothing?
false
Any <: Nothing?
<notype> <: Nothing?
false
false
false
false
B <: A?
B <: Nothing?
<notype> <: Nothing?
false
Any <: Nothing?
<notype> <: Nothing?
false
false
false
Any <: A?
Any <: Nothing?
<notype> <: Nothing?
false
false
false
false
false
关于scala - 根据参数值和函数参数类型推断通用父类(super class)型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3131661/
当使用模板模板参数时,我如何推断或删除模板模板的模板类型? 考虑以下 SSCCE: #include #include #include using namespace std; templat
假设我有一些特质: trait A[T] { def foo: T } 一个扩展它的类: class B[T](t: T) extends A[T] { def foo = t } 以及父特征的子特征
一边玩-rectypes在某些时候选择 OCaml 我只是迷路了。 这个表达式几乎可以打字: # fun x -> x x;; - : ('a -> 'b as 'a) -> 'b = 但是这里 O
我正在编写一个类似 CRUD 的应用程序,并且通过主键进行大量查找(主键可以有不同的类型)。所以我定义了以下类型类: {-# LANGUAGE MultiParamTypeClasses #-} cl
我已经创建了关系 A 'is functional parent of' B并定义 'has functional parent'作为 'is functional parent of' 的倒数. '
给定一个使用 Kotlin 版本 1.3.61 和 JOOQ 版本 3.13.1 的系统,这样的方法会构建 union正常查询: val selectCommonPart = coalesce
考虑以下错误代码: fun x = if (null x) then 0 else (take 50 x) : (fun (drop 50 x)) 我注意到,我可以毫无问题地将它加载到
给定一个具有以下类型的函数 a: a::x -> Bool 和以下类型的另一个函数 b: b::Bool -> y 我正在尝试找出推断以下函数类型的步骤: c =\d -> d a b 有人可以帮助解
我正在尝试使用 Infer 工具来分析我的应用代码。我关注了these steps每次我尝试运行 infer -- gradle build 时,我都会收到以下错误: infer -- gradle
所以我制作了这个模板来定义内联仿函数: template struct AsFunctor { template std::invoke_result_t operator()(A
是否可以推断 CRTP 基类中模板化成员函数的返回类型? 虽然推断参数类型效果很好,但它因返回类型而失败。考虑以下示例。 #include template struct base { tem
使用 Series.interpolate 很容易在 Pandas.DataFrame 中插入值,如何进行外推? 例如,给定一个如图所示的 DataFrame,我们如何将它外推 14 个月到 2014
我想知道为什么这不起作用(缺少参数类型)? Seq(1,2,3).toSet.map(_ + 1) 但这确实: val foo = Seq(1,2,3).toSet foo.map(_ + 1)
我没有必要使用 SQLite3 shell 工具来维护一个小型数据库。我正在使用 -header -ascii标志,尽管据我所知,这适用于任何输出选择。我正在寻找一种方法来避免对返回的任何一个值的类型
我有以下组件 type PropTypes = { items: T[], header: (item: T) => React.Element, body: (item: T) => R
我想在 Eclipse/JSDT 中指定实例变量的类型,如下例所示: /** * @constructor */ function A() { /** @type Node */
我正在用 Python 编写一个方法,它看起来像这样: def rgb_to_grayscale(image): print(image.shape) pass 此处预期的类型是 nu
我有一个 my_values 数组,我正在尝试为其推断 true_values 数组中最接近、较小的值。使用下面的 find_nearest 函数并不能完成我想要的。我如何追加它以找到最近的、较小的值
在下面的代码中: template int b(int q, const std::array& types) { int r = q; for (int t : types)
在 Pandas DataFrame 中插入 NaN 单元非常容易: In [98]: df Out[98]: neg neu pos av
我是一名优秀的程序员,十分优秀!