Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

inheritance - What does "trait A <: B" mean?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

NOTE As of Scala 2.12.5 using <: for extends is deprecated

scala -deprecation -e 'trait B; trait A <: B'
/var/folders/0w/kb0d3rqn4zb9fcc91pxhgn8w0000gn/T/scalacmd2374381600671257557.scala:1: warning: Using `<:` for `extends` is deprecated
trait B; trait A <: B
                 ^
one warning found

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...