5

Possible Duplicate:
Why does one select Scala type members with a hash instead of a dot?

I sometimes see a type parameter like:

class Test[K] {
  type T = Foo[K#Bar]
}

Can someone please explain what the '#' means in this type parameter ? Is it some kind of restriction for K ?

Community
  • 1
  • 1
John Threepwood
  • 15,593
  • 27
  • 93
  • 149

2 Answers2

3

No, the # is a type projection. In your case however that does not work because K does not define any BAR type.

trait A  { 
           type T 
           def apply():T 
}

trait MyClass[X<:A] { 
               type SomeType = X#T 
               def applySeq():Traversable[SomeType] 
}


class AImpl extends A { 
      type T=Int 
      def apply():Int = 10
}

class MyClassImpl extends MyClass[AImpl] {
         def applySeq(): Traversable[SomeType] = List(10)
}

That basically let you use the type T in A inside MyClass .

In fact, also the following compile:

class MyClassImpl extends MyClass[AImpl] {def applySeq(): Traversable[Int] = List(10)}
Edmondo
  • 19,559
  • 13
  • 62
  • 115
0

'#' is used to project out a type enclosed in another type. See this answer to the question "Why does one select Scala type members with a hash instead of a dot"?

Community
  • 1
  • 1
michid
  • 10,536
  • 3
  • 32
  • 59