scala - Typesafe Swing events—"The outer reference in this type test cannot be checked at run time" -


i implementing swing component , want overcome #### untypedness of reactor. thought work:

trait foo[a] extends scala.swing.publisher {   final case class bar(parent: vector[a], children: a*) extends scala.swing.event.event }  trait test {   val foo: foo[int]    foo.reactions += {     case foo.bar(parent, children) => {       println(parent.sum - children)     }   } } 

unfortunately gives me 2 compiler warnings:

the outer reference in type test cannot checked @ run time.   final case class bar(parent: vector[a], children: a*) extends scala.swing.event.event                    ^ outer reference in type test cannot checked @ run time.     case foo.bar(parent, children) => {                 ^ 

should ignore these warnings? can suppress them? should change design?

in scala, inner classes "path-dependent".

i'll use code example. if have 2 foo[int]s, called foo , bar, foo.bar different type bar.bar. note differs java's idea of inner classes, foo.bar , bar.bar same type.

in case, jvm doesn't support inner classes directly, in both java , scala, class bar compiles jvm class called foo$bar. instances of inner classes almost contain reference owner - "outer reference".

now, when you've got pattern match on path dependent type (like 1 in code), scala compiler produce bytecode 2 things: check class (so check object received instance of foo$bar), , check outer reference (so check outer reference of object received foo).

however, in code, compiler can't find way check outer reference, because you've declared inner class final.

so, if ignore warning, pattern match instances of foo$bar, if don't belong foo. you'll have better idea me whether problem.

or, can fix making inner class non-final.

p.s, i'm not entirely sure why scala compiler can't check outer references on final inner classes, i've found if inner class final, outer$ private, whereas if it's non-final, outer$ public. might worth poke around in compiler's internals, figure out why is.

update

it turns out known issue - si-4440. scala compiler drops outer references final inner classes, if they're not used (which vaguely legitimate, there's no possibility of subclasses using them either). 1 positive consequence of outer classes can garbage collected whilst inner classes still in use, scala devs reluctant reintroduce outer references fear of introducing memory leaks.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -