views:

370

answers:

4

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.

+2  A: 

Yes, well, almost, see this article for a little more information. From the language spec, we see the following definition:

We define two relations between types.

Type equivalence T ≡ U T and U are interchangeable in all contexts.

Conformance T <: U Type T conforms to type U .

Edit: Looking into the language spec it appears that <: and extends are the same, in particular it is defined as:

ClassTemplateOpt ::= Extends ClassTemplate | [[Extends] TemplateBody]
TraitTemplateOpt ::= Extends TraitTemplate | [[Extends] TemplateBody]
Extends ::= ‘extends’ | ‘<:’
Paul Wagland
+10  A: 

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.

retronym
Depends which part of the spec you look at! The syntax summary appendix says, "Extends ::= ‘extends’ | ‘<:’"
Matt R
I raised a bug on the spec: https://lampsvn.epfl.ch/trac/scala/ticket/2953
retronym
See also http://lampsvn.epfl.ch/trac/scala/export/20327/scala/branches/devel-base-2.8.0/SIP/virtual-traits/sip-0000X.xhtml
Matt R
+1  A: 

Looking at the Scala Language Specification, it seems to mean the same thing. The description for trait only mentions the trait A extends B syntax. But the Scala syntax summary uses extends and <: interchangeably for trait definitions:

TraitTemplateOpt ::= Extends TraitTemplate | [[Extends] TemplateBody]
Extends ::= ‘extends’ | ‘<:’ 
huynhjl
+2  A: 

The <: syntax is reserved for future use in virtual classes (which are not implemented yet).

Adriaan Moors