Scala: Mixing traits with private fields

Posted by Vilius Normantas on Stack Overflow See other posts from Stack Overflow or by Vilius Normantas
Published on 2011-03-05T12:26:54Z Indexed on 2011/03/05 15:24 UTC
Read the original article Hit count: 225

It's not much of a question, it's rather my excitement that it's possible at all! I wrote this little example just to prove the opposite - I expected either a compiler error or one of the values (111 or 222, I wasn't sure).

scala> trait T1 { private val v = 111; def getValueT1 = v }
scala> trait T2 { private val v = 222; def getValueT2 = v }
scala> class T12 extends T1 with T2                        
scala> val t = new T12                                     
scala> t.getValueT1                                        
res9: Int = 111
scala> t.getValueT2                                        
res10: Int = 222

Why doesn't the v get overridden? Off course this works only as long as vs are private, but still.

© Stack Overflow or respective owner

Related posts about scala

Related posts about multiple-inheritance