如何解决结构类型扩展了另一种结构类型
为什么不允许以下内容? (2.12):
<?PHP
$video_tkn = "3D";
$reklam_json = json_decode($siteayar->reklam_json);
$video = json_decode($reklam_json->video);
$video_arr = array();
foreach ($video as $v) {
if ($v->video_tur==$video_tkn) {
$video_arr[] = $v->fiyat;
}
}
print_r($video_arr[0]);
对我来说很自然,type Foo <: {def foo(): Unit}
type Bar <: {def bar(): Unit} with Foo
应该同时具有Bar
和foo()
。
解决方法
似乎可以使用括号
scala> type Foo <: {def foo(): Unit}
| type Bar <: ({def bar(): Unit}) with Foo
type Foo
type Bar
,
type Foo <: {def foo(): Unit}
是type Foo <: AnyRef{def foo(): Unit}
。
因此,尽管type Bar <: {def bar(): Unit} with Foo
是不可解析的,但使用AnyRef
和不同的顺序,它就可以工作
type Foo <: {def foo(): Unit}
type Bar <: Foo with AnyRef{def bar(): Unit}
val b: Bar = ???
b.foo()
b.bar()
也可以使用方括号(和直接订购)
type Foo <: {def foo(): Unit}
type Bar <: ({def bar(): Unit}) with Foo
val b: Bar = ???
b.foo()
b.bar()
实际上type Bar <: {def bar(): Unit} with Foo
违反规范,而type Bar <: ({def bar(): Unit}) with Foo
符合规范,因为{def bar(): Unit}
不是SimpleType
,而({def bar(): Unit})
是SimpleType
。
CompoundType ::= AnnotType {‘with’ AnnotType} [Refinement]
| Refinement
AnnotType ::= SimpleType {Annotation}
SimpleType ::= ............
| ‘(’ Types ‘)’
Types ::= Type {‘,’ Type}
Type ::= ...........
| CompoundType
| ...........
https://scala-lang.org/files/archive/spec/2.13/03-types.html#compound-types
,规范不允许这样做,该规范将{ ...}
部分称为精化,可能都具有with子句。相反,它还可以。
这当然是重言式的,因为它没有说明为什么规范不允许这样做。似乎这里没有深入的了解,只是这不是允许您编写类型的方式。您应该能够以其他方式表达相同的意图。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。