- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
sealed trait Desc {
type T
}
trait Dataset[A] {
def toDS[A] = new Dataset[A] {}
}
trait DataFrame {}
sealed trait DFDesc extends Desc {
type T = Dummy
}
sealed trait DSDesc[A] extends Desc {
type T = A
}
trait JobConstruction {
def apply(desc: Desc): Job[desc.T]
}
sealed trait Job[DescType] {
def description: Desc { type T = DescType }
}
abstract class DSJob[V] extends Job[V] {
def result(con: JobConstruction): Dataset[V]
}
abstract class DFJob extends Job[Dummy] {
def result(con: JobConstruction): DataFrame
}
trait Dummy {}
case class SampleDFDesc() extends DFDesc
case class SampleDFJob(description: SampleDFDesc) extends DFJob {
override def result(con: JobConstruction) = new DataFrame {}
}
case class SampleDSDesc() extends DSDesc[Int]
case class SampleDSJob(description: SampleDSDesc) extends DSJob[Int] {
override def result(con: JobConstruction) = new Dataset[Int] {}
}
object Main {
val sampleConst = new JobConstruction {
override def apply(desc: Desc): Job[desc.T] = desc match {
case desc2: SampleDFDesc => SampleDFJob(desc2)
case desc2: SampleDSDesc => SampleDSJob(desc2)
}
}
}
/tmp/sample.scala:73: error: type mismatch;
found : this.SampleDFJob
required: this.Job[desc.T]
case desc2: SampleDFDesc => SampleDFJob(desc2)
^
/tmp/sample.scala:74: error: type mismatch;
found : this.SampleDSJob
required: this.Job[desc.T]
case desc2: SampleDSDesc => SampleDSJob(desc2)
case class SampleDepDesc(df: SampleDFDesc) extends DSDesc[Int]
case class SampleDepJob(description: SampleDepDesc) extends DSJob[Int] {
override def result(con: JobConstruction): Dataset[Int] = con(description.df).result(con).toDS[Int]
}
最佳答案
问题分析
如果你写 sampleConst
,错误会以更有趣的方式表达出来。像这样:
object Main {
val sampleConst = new JobConstruction {
override def apply(desc: Desc): Job[desc.T] = {
val result = desc match {
case desc2: SampleDFDesc => SampleDFJob(desc2)
case desc2: SampleDSDesc => SampleDSJob(desc2)
}
result
}
}
type mismatch;
found : Product with Serializable with main.Job[_ >: main.Dummy with Int]{def description: Product with Serializable with main.Desc{type T >: main.Dummy with Int}}
required: main.Job[desc.T]
Note: Any >: desc.T (and Product with Serializable with main.Job[_ >: main.Dummy with Int]{def description: Product with Serializable with main.Desc{type T >: main.Dummy with Int}} <: main.Job[_ >: main.Dummy with Int]), but trait Job is invariant in type DescType. You may wish to define DescType as -DescType instead. (SLS 4.5)
SampleDFJob
,
SampleDSJob
,
SampleDFDesc
和
SampleDSDesc
已被删除:
sealed trait Desc {
type T
}
sealed trait DFDesc extends Desc {
type T = Dummy
}
sealed trait DSDesc[A] extends Desc {
type T = A
}
trait JobConstruction {
def apply(desc: Desc): Job[desc.T]
}
sealed trait Job[DescType] {
def description: Desc { type T = DescType }
}
class DSJob[V] extends Job[V]
class DFJob extends Job[Dummy]
trait Dummy {}
object Main {
val sampleConst = new JobConstruction {
override def apply(desc: Desc): Job[desc.T] = {
val result = desc match {
case desc2: DFDesc => new DFJob
case desc2: DSDesc[Int] => new DSJob[Int]
}
result
}
}
}
type mismatch;
found : main.Job[_1] where type _1 >: main.Dummy with Int
required: main.Job[desc.T]
main.Job[_ >: main.Dummy with Int]
进入
desc.T
.
result
取决于模式匹配的情况(在第一种情况下,我们有一个
Dummy
,在第二种情况下,我们有一个
Int
)。由于 Scala 是静态类型的(至少在编译期间),它会尝试组成一个返回类型,它是所有可能类型的“公分母”(或者更确切地说,父类型)。它找到的最好的东西是
_ >: main.Dummy with Int
,它是“在模式匹配中找到的任何类型的父类型的任何类型”(
main.Dummy
和
Int
)。
desc.T
的原因Scala 无法在编译时确认返回的类型始终是
同 (因为
DescType
是不变的)如
Job[desc.T]
.确实,
desc.T
来自
SampleDFDesc.T
或
SampleDSDesc.T
,而返回类型将为
DescType
, 并不能保证这两种类型是相同的(如果
SampleDSJob
扩展
DSJob[String]
呢?)
desc.T
的类型相同,然后您可以使用
asInstanceOf
指定显式转换,像这样:
object Main {
val sampleConst = new JobConstruction {
override def apply(desc: Desc): Job[desc.T] = (desc match {
case desc2: SampleDFDesc => SampleDFJob(desc2)
case desc2: SampleDSDesc => SampleDSJob(desc2)
}).asInstanceOf[Job[desc.T]]
}
}
Job
类使
DescType
可以逆变(
-DescType
),你可以写成
Job.apply
方法改为具有以下签名:
def apply(desc: Desc): Job[_ <: desc.T]
关于scala - 匹配/大小写上的路径相关打字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34907973/
如何使用打字模块来创建可以是某些字符串的类型? 例如。假设我需要一个类型 CondOperator ,可以是以下任何字符串: ['=', '>', '=', '', '!='] 我希望 CondOpe
我目前正在从事一个处理语言学习(即德语、中文等...)的项目,其中有一个功能我们遇到了问题 - 简而言之,我们正在尝试显示“幽灵” "文本(非常淡的灰色)并允许用户键入覆盖此文本。 该项目将有数千个不
typing 模块提供了一些方便的功能,以实现更好的可读性和对键入代码正确性的信心。 最好的功能之一是您可以编写如下内容来描述具有指定元素类型的输入字典。 def myFun(inputDict:Di
我想创建一个 Array 类型,它应该是可订阅 并且是 typing.List 和 numpy 的联合体.ndarray 类型。我知道 numpy 没有 stub ,但是 those numpy st
我想写一个groupBy功能,到目前为止,类型系统对我的工作不太满意。 export function group( data: T[], groupBy: keyof T ): {
我正在努力解决打字问题并提出以下问题 您在项目的 node_modules 根文件夹下找到的 typings 文件夹是什么。这是 tsc 查找 .d.ts 文件的默认位置吗?我如何在源文件中使用它们。
在打字时使用 typing.Any 和 object 有什么区别吗?例如: def get_item(L: list, i: int) -> typing.Any: return L[i] 相
我刚开始使用 TypeScript,所以请记住。我正在尝试在 React/TS 中实现一个简单的文件上传。一般来说,我认为我不明白如何初始化对象,比如在 useState 中,并正确处理可能性。例如,
我想这是python 3.7(不确定)附带的,不仅可以将变量名传递给函数,还可以传递变量的类型。我想知道的是是否有可能传递特定类的类型。 你可以通过同样的方式: def foo_func(i: int
有没有办法在 TypeScript 类中拥有动态对象属性,并为 TypeScript 添加动态类型? 我见过类似的问题,但没有一个像这样的完整示例 - interface IHasObjectName
我正在尝试将类型添加到一些数字 Racket 代码中,希望能使其更快,但我在下面的代码中遇到了 for/list 宏扩展的问题。 (: index-member ((Listof Any) (List
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 2年前关闭。 Improve this
我在 Scala 中有一个打字问题的小问题。在 Haskell 中,我可以这样做: add :: (Num a) => (a,a) -> (a,a) -> (a,a) 这样,我就可以扔进add任何支持
我想实现这个。 当文本表单字段处于非事件状态时,其背景和填充颜色将为灰色。但是当我打字或它处于事件模式时,它的背景颜色将为白色。 如何实现这种行为? 最佳答案 试试这个: class CustomTe
我想实现这个。 当文本表单字段处于非事件状态时,其背景和填充颜色将为灰色。但是当我打字或它处于事件模式时,它的背景颜色将为白色。 如何实现这种行为? 最佳答案 试试这个: class CustomTe
这可以模拟击键: import pyautogui pyautogui.typewrite('hello world!', interval=0.1) 除了: 它写的是 hello world§(使用
我正在寻找 python 中的东西因为它是动态类型而更容易编程的例子? 我想将它与 Haskell 类型系统进行比较,因为它的静态类型不像 c# 或 java 那样妨碍。我可以像在 python 中一
当运行 typings 命令时,我总是得到错误: AppData\Roaming\npm\node_modules\typings\node_modules\strip-bom\index.js:2
我正在学习 Ruby,我遇到了一个关于打字的主要概念问题。请允许我详细说明为什么我不理解范式。 假设我像您在 Ruby 中一样为简洁的代码进行方法链接。我必须准确地知道链中每个方法调用的返回类型是什么
只是关于打字的快速问题。 如果我输入 ghci :t [("a",3)]我回来了[("a",3)] :: Num t => [([Char], t)] 在文件中,我将类型定义为: type list
我是一名优秀的程序员,十分优秀!