Sebastien Diot
Sebastien Diot

Reputation: 7359

How do you define a package-private *trait* in Scala?

In Java, it is possible to create package-private interfaces. Looking at them with javap, you see that they lack the "public" visibility.

In Scala, you can declare a trait as private[package] or protected[package], but looking at in with javap, it is still public.

So how do you create a package-private trait in Scala?

While the Scala compiler respects the visibility, my problem is that my API will probably be accessed from Java too, and I don't want to expose my internal implementation to Java.

Upvotes: 14

Views: 4790

Answers (2)

Lex
Lex

Reputation: 1184

It is not possible to create Java package private modifiers using Scala. However you can freely mix Java and Scala files in a Scala project. So the easiest solution is to create a Java class/interface and then extend it in Scala.

Upvotes: 10

John Cheng
John Cheng

Reputation: 569

I believe this is the answer to your question

http://www.scala-lang.org/node/10488

private has a subtly special status in the language specs of both Scala and Java. Check out the discussion of private vs qualified private in the Modifiers section of the SLS. In short, private is the same as Java private, whereas private[foo] is not marked private in the bytecode, but simply involves a compile-time access check.

I don't believe you can truly make a trait package private once it is compiled into bytecode.

Upvotes: 13

Related Questions