Package-private scope in Scala visible from Java -


i found out pretty weird behaviour of scala scoping when bytecode generated scala code used java code. consider following snippet using spark (spark 1.4, hadoop 2.6):

import java.util.arrays; import java.util.list;  import org.apache.spark.sparkconf; import org.apache.spark.api.java.javasparkcontext; import org.apache.spark.broadcast.broadcast;  public class test {     public static void main(string[] args) {         javasparkcontext sc =              new javasparkcontext(new sparkconf()                                 .setmaster("local[*]")                                 .setappname("test"));          broadcast<list<integer>> broadcast = sc.broadcast(arrays.aslist(1, 2, 3));          broadcast.destroy(true);          // fails java.io.ioexception: org.apache.spark.sparkexception:          // attempted use broadcast(0) after destroyed         sc.parallelize(arrays.aslist("task1", "task2"), 2)           .foreach(x -> system.out.println(broadcast.getvalue()));     } } 

this code fails, expected voluntarily destroy broadcast before using it, thing in mental model should not compile, let alone running fine.

indeed, broadcast.destroy(boolean) declared private[spark] should not visible code. i'll try looking @ bytecode of broadcast it's not specialty, that's why prefer posting question. also, sorry lazy create example not depend on spark, @ least idea. note can use various package-private methods of spark, it's not broadcast.

any idea of what's going on ?

if reconstruct issue simpler example:

package yuvie  class x {   private[yuvie] def destory(d: boolean) = true } 

and decompile in java:

[yuvali@localhost yuvie]$ javap -p x.class  compiled "x.scala" public class yuvie.x {   public boolean destory(boolean);   public yuvie.x(); } 

we see private[package] in scala becomes public in java. why? comes fact java private package isn't equivalent scala private package. there nice explanation in post:

the important distinction 'private [mypackage]' in scala is not java package-private, looks it. scala packages hierarchical, , 'private [mypackage]' grants access classes , objects up to "mypackage" (including hierarchical packages may between). (i don't have scala spec reference , understating here may hazy, i'm using [4] reference.) java's packages not hierarchical, , package-private grants access classes in package, subclasses of original class, scala's 'private [mypackage]' not allow.

so, 'package [mypackage]' both more , less restrictive java package-private. both reasons, jvm package-private can't used implement it, , option allows uses scala exposes in compiler 'public.'


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -