Implementing inner traits in Scala like we do with inner interfaces in Java -


this code in java compiles without errors:

interface t {     interface q {     } }  class c implements t.q { } 

whereas code in scala not:

trait t {     trait q {     } }  class c extends t.q { } 

what correct translation (if exists) of java code listing scala?

theoretical explanations language design welcome.

the inner type q defined specific instance implementation of t trait. since scala has path-dependent types, each instance of t have own subtrait q.

scala> trait t {      |   trait q      | } defined trait t  scala> class c extends t {      |   def getq: this.q = new this.q {}      | } defined class c  scala> val inc = (new c).getq inc: c#q = c$$anon$1@3f53073a   scala> val c = new c c: c = c@1a7e4ff0  scala> new c.q {} res4: c.q = $anon$1@36bbb2f5 

if need interface generic behavior clients implement, , not dependent on specific c instance, should define within object

scala> object t {      |   trait q {      |     def implementme: unit      |   }      | } defined module t  scala> val int = new t.q {      |   def implementme = println("implemented!")      | } int: t.q = $anon$1@20f2a08b  scala> int.implementme implemented! 

why path-dependent types?

as design reasons, here


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -