tags:

views:

277

answers:

2

Hi All, I've got a simple question for you :) I have the following Scala code, which compiles and gives an error. Actually I am expecting the code not be compiled as it has an abstract type CT. And the error is even more confusing as the scala says it can't find the type CT.

class Currency {   type ct=Currency } 
val c = new Currency
println(c.ct)

error: value ct is not a member of Currency

I appreciate your comments,

Thanks, -A

PS - Using sala 2.7.7

+2  A: 

In this case type ct is NOT an abstract type but a simple type alias for Currency type. Hence that expression SHOULD compile.

Regarding the println expression, you are using c.ct as a value to be printed (by calling its toString). This is NOT correct because c.ct is not a value but a type (path dependent type). Hence a valid (but somewhat meaningless) expression involving c.ct could be:

val a: c.ct = new Currency

Hope that helps.

Mushtaq Ahmed
I added a comment with regard of this question.
Ali
Regarding the comment: Well, even I had come across that and thought it was a bug. But it seems that there are some reasons why it exists:http://lampsvn.epfl.ch/trac/scala/ticket/1753
Mushtaq Ahmed
+12  A: 

Have a close look at the error message: "value ct is not a member of Currency". The "value" here is significant. Currency has a type member ct, but not a value member ct. So c.ct is a type, not an expression.

Martin Odersky
@Rahul, I believe he is. Check his other answer (also on scala)
Pablo Fernandez
Downvoted as unhelpful. Too many uses of the word "member". Also, add some extraneous example code to prove your formatting capability.
Mitch Blevins