Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 369
Sebastien Diot Avatar asked Aug 23 '11 20:08

Sebastien Diot


People also ask

How do you declare a trait in Scala?

In scala, trait is a collection of abstract and non-abstract methods. You can create trait that can have all abstract methods or some abstract and some non-abstract methods. A variable that is declared either by using val or var keyword in a trait get internally implemented in the class that implements the trait.

What is private [] in Scala?

In Scala,private[this] is object private,which makes sure that any other object of same class is unable to access private[this] members.

How do you call a trait in Scala?

In Scala, one trait can inherit another trait by using a extends keyword. Traits support multiple inheritance. In Scala, a class can inherit both normal classes or abstract class and traits by using extends keyword before the class name and with keyword before the trait's name.

What is trait in Scala?

A Trait is a concept pre-dominantly used in object-oriented programming, which can extend the functionality of a class using a set of methods. Traits are similar in spirit to interfaces in Java programming language. Unlike a class, Scala traits cannot be instantiated and have no arguments or parameters.


1 Answers

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.

like image 148
John Cheng Avatar answered Oct 31 '22 11:10

John Cheng