什么是“特质A<:B”。意思? [英] What does "trait A <: B" mean?

查看:223
本文介绍了什么是“特质A<:B”。意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scala中,做什么

  trait A<:B 

是什么意思?是否与

相同

 特质A延伸B 



编辑添加:我熟悉类型参数的语法,以及<:表示在该上下文中。但是,在上面的例子中,我认为 A 是要声明的特征的名称,而不是类型参数。

 〜/ code / scratch:scala -Xprint:typer -e'trait B; trait A<:B'
// snip
abstract trait B扩展scala.AnyRef;
abstract trait用这个扩展java.lang.Object.B

~ / code / scratch:scala -Xprint:typer -e'trait B; trait A extends B'
// snip
abstract trait B extends scala.AnyRef;
abstract trait A使用this扩展java.lang.Object.B

规范没有'在5.3.3特征中解释这一点。但语法摘要确实提到了这一点。

  TraitDef :: = id [TypeParamClause] TraitTemplateOpt 
TraitTemplateOpt :: = Extends TraitTemplate | [[Extends] TemplateBody]
Extends :: ='extends'| '<:'

UPDATE 它是在 r14632 。使用编译器选项 -Xexperimental ,它将特征标记为抽象,用于建议的语言特征虚拟特征。如果没有 -Xexperimental ,它只是'extends'的同义词,只允许用于特征。


In Scala, what does

trait A <: B

mean? Is it just the same as

trait A extends B

?

Edited to add: I'm familiar with the syntax for type parameters, and what <: means in that context. However, in the above example it would seem to me that A is the name of the trait being declared, not a type parameter.

解决方案

Seems to compile to the same thing.

 ~/code/scratch: scala -Xprint:typer -e 'trait B; trait A <: B'
          // snip
          abstract trait B extends scala.AnyRef;
          abstract trait A extends java.lang.Object with this.B

 ~/code/scratch: scala -Xprint:typer -e 'trait B; trait A extends B'
          // snip
          abstract trait B extends scala.AnyRef;
          abstract trait A extends java.lang.Object with this.B    

The spec doesn't explain this in "5.3.3 Traits". But the Syntax Summary does mention this.

TraitDef ::= id [TypeParamClause] TraitTemplateOpt 
TraitTemplateOpt ::= Extends TraitTemplate | [[Extends] TemplateBody]
Extends ::= ‘extends’ | ‘<:’

UPDATE It was introduced in r14632. With the compiler option -Xexperimental it marks the trait as abstract, for use with a proposed language feature Virtual Traits. Without -Xexperimental, it is a synonym for 'extends' that is allowed only for traits.

这篇关于什么是“特质A&lt;:B”。意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆