- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人可以解释type
关键字和 #
运算符在scala中工作以及如何使用它?
请看例子。
//Example1
scala> type t1 = Option.type
defined type alias t1
//Shouldn't this work since previous example simply works?
scala> type t2 = String.type
<console>:7: error: type mismatch;
found : String.type
required: AnyRef
type t2 = String.type
^
//lets define custom trait T
scala> trait T
defined trait T
//... and obtain it's type like in Example1.
//Shouldn't this work since previous Example1 simply works?
scala> type t3 = T.type
<console>:7: error: not found: value T
type t3 = T.type
^
//Lets define some value of type T
scala> val v4 = new T{}
v4: T = $anon$1@5c3e8c76
//and obtain it's type (works)
scala> type t4 = v4.type
defined type alias t4
//this doesn't work
scala> type t4_1 = (new T{}).type
<console>:1: error: identifier expected but 'new' found.
type t4_1 = (new T{}).type
//as well as this (doesn't work)
scala> type t5 = "abc".type
<console>:1: error: identifier expected but string literal found.
type t5 = "abc".type
^
//but this compiles well
scala> val v6 = "abc"
v6: String = abc
scala> type t6 = v6.type
defined type alias t6
//lets create some values of created types:
scala> type t1 = Option.type
defined type alias t1
scala> val v1_1 = Some(10)
v1_1: Some[Int] = Some(10)
scala> type t7 = v1_1.type
defined type alias t7
scala> val v7:t7 = null
v7: t7 = null
scala> val v7_1:t7 = v1_1
v7_1: t7 = Some(10)
scala> val v7_2:t7 = Some(10)
<console>:9: error: type mismatch;
found : Some[Int]
required: t7
(which expands to) v1_1.type
val v7_2:t7 = Some(10)
^
//next let's try # operator
scala> class X[A,B](a:A,b:B)
defined class X
//doesn't work
scala> type xa = X[A,B]#A
<console>:8: error: not found: type A
type xa = X[A,B]#A
^
<console>:8: error: not found: type B
type xa = X[A,B]#A
^
//but such approach works:
scala> trait X2[C]{
type A
type B
val c:C
}
defined trait X2
scala> type xa2_1 = X2[String]#A
defined type alias xa2_1
scala> type xa2_2[M] = X2[M]#A
defined type alias xa2_2
最佳答案
首先,您对 type
的问题:
类型声明的右侧必须是具有稳定路径的类型的名称。因此,一个一个地举出你的例子:
type t1 = Option.type
Option
类型的别名。对象,而不是
Option
类(class)。
type t2 = String.type
String
目的。这个错误有点奇怪,因为 String 是一个 Java 类,因此在不同的规则下运行(因为 Java 类从来没有同伴)。
type t3 = T.type
type t4 = v4.type
v4
命名的对象的单例类型。 .它不引用任何类型 T 的实例,甚至不引用由您的
new T{}
创建的匿名类的任何实例。表达。它指的是仅由
v4
表示的类型和
null
,即它们是该类型唯一允许的值。
type t4_1 = (new T{}).type
object
s , 和
val
s,它是稳定的)。
type t5 = "abc".type
type t6 = v6.type
v6
是一个稳定的标识符。
t6
是仅由名称
v6
引用的特定 String 实例所包含的类型(和
null
)。
type v6 = v1_1.type
val v7: t7 = null
null
是
t7
类型的有效值
val v7_1:t7 = v1_1
val v7_2:t7 = Some(10)
==
到
v7
,它不是
eq
到它),因此不是这种类型的成员。
#
:
class X[A,B](a:A,b:B)
A
和
B
是类型参数。他们不能在类外被提及。您可以将它们视为具有
private[this]
的抽象类型别名。能见度,虽然这不是很准确。
type xa = X[A,B]#A
type xa2_1 = X2[String]#A
A
引用公共(public)类型别名,可以在类外通过名称引用。请注意,这种特殊情况非常无用,因为您对这种类型一无所知。如果你的特质
X2
有一个返回
A
类型值的方法,你可以做类似的事情
val aFromX2: xa2_1 = x2instance.methodThatReturnsAnA
X2[String]
的实例因为不能保证这两个
A
s 指的是同一类型!另一方面,如果你有一个具体的实例,你可以这样做:
def passAroundA(x2instance: X2[String]) {
type x2a = x2instance.A // note dot, not #
val a: x2a = x2instance.methodThatReturnsAnA
x2instance.methodThatTakesAnA(a)
}
A
是什么实际上,我们知道这两种方法使用相同的类型——无论是固定在
x2instance
的 build 。
关于scala - scala中 `type`和 `#`关键字的调查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12959438/
是否有任何工具可以检查现有的 squashfs 图像并找出每个文件的压缩率? 如果它可以帮助我估计一个巨大的可执行文件中静态链接符号的闪存使用情况,则可以加分。 最佳答案 7zip 程序可以提供这些信
我有自定义调查,在 RootWeb.Properties 中有到期日期。如何在到期日期后将所有想要回复调查的用户(单击“回复调查”或使用指向 NewForm.aspx 的直接链接)重定向到包含一些自定
是否可以通过查询字符串发送答案?像这样的东西: HTTP://survey/1?ASW1=test 最佳答案 是的,请参阅手册 - https://manual.limesurvey.org/Work
我问了一个question关于如何在调试时调查 XMLWriter 对象的内容。我正在尝试检查从内存流创建的 XmlReader 对象的内容,其方式与链接问题的答案中给出的方式类似。但是我收到 Una
如何在调试时查看完全填充的 XmlWriter 对象的 XML 内容。我的 Silverlight 应用程序不允许实际写入文件和检查内容。 最佳答案 让它写入 MemoryStream或 String
我今天才开始使用 AB。阅读一些新的 AB 教程,并尝试尝试对我的网站进行负载测试。 使用它几次后,我收到了大量失败的请求。你能解释一下失败的请求是什么意思吗?我该如何针对此问题进行更多调查? 示例
当我运行 project 时与 valgrind我收到错误报告。如果我不使用 Valgrind,那么程序看起来是正常的,但是当 Valgrind 报告它时,我确定有问题。 ==21286== Inva
我在一页上有 36 个常见问题解答和答案。每个答案都包含在一个可折叠元素中。我的目标是让用户在常见问题解答中单击是或否,然后我通过 GTM/GA 跟踪该值。但是,当我当前单击是或否时,该值显示“感谢您
我们在 aws 上的 hdfs 之上有一个 spark 集群,具有 2 个核心节点和 1 个主节点。最近我发现所有作业都失败了,因为两个核心节点上的磁盘都已满。经过进一步调查发现: /data/var
我正在构建一个 Web 应用程序,它基本上允许“管理员”创建具有任意数量和组合的表单元素(复选框、组合框、文本字段、日期字段、单选组等)的表单。 “用户”将登录此应用程序并完成管理员创建的表格。 我们
想象一下这样的情况。您获得了一些遗留代码或获得了一些新框架。您需要尽快调查并了解如何使用此代码。没有机会向以前的开发人员寻求帮助。什么是最佳实践/方法/方式/步骤/工具(首选 .NET Framewo
我正在尝试从在 MacOS 上运行的 docker 容器访问 host-x.com,但它失败了: $ docker run ubuntu:latest \ /bin/bash -c \ '
我最近研究了 WinForms 应用程序中的一些 .NET“内存泄漏”(即意外的、挥之不去的 GC 根对象)。加载然后关闭一个巨大的报告后,即使经过几次 gen2 收集,内存使用量也没有按预期下降。假
如何允许具有读取权限的用户回复 Sharepoint 2013 调查? 我尝试过仅读取访问权限,但无法获取“回复调查”链接。我还尝试了“贡献访问”选项,这一次,我得到了“回复调查”按钮。当我尝试填写调
如何允许具有读取权限的用户响应 Sharepoint 2013 调查? 我试过只有读取权限,但无法获得“回复调查”链接。我也尝试了“贡献访问”选项,这次,我得到了“响应调查”按钮。当我尝试填写调查详细
目前我正在深入研究 CLR 并尝试找到我的托管对象的适当大小。 我有两个简单的类型: XClass class XClass { public XStruct StructField
我正在调查在由 mesos marathon 编排的 docker 容器中运行的 java 应用程序的内存不足问题。 容器设置为2GB内存 JVM 堆显式设置为最小 1Gb 和最大 1.5GB 持续测
在使用/FA 选项编译使用此虚拟类的代码时 class A { public: A() {} int Initialize() { return 0; } }
我正尝试在 Symfony 3.2.4 和 PHP 5.6.28 中为我的应用程序创建动态调查/表单包 我有 3 个实体:调查/问题/答案 第一步是为每个用户创建一个调查。问题必须与相关的答案字段一起
我正在尝试将一个简单的调查嵌入到弹出窗口中并将其与我的应用程序集成。弹出窗口需要包含一个调查问题,该问题将使用两个选项单选按钮捕获用户的响应。一旦用户选择单选选项,我需要触发一个事件以启用提交按钮。单
我是一名优秀的程序员,十分优秀!