Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the guarantees for scala access qualifiers?

I have a class with this code:

package shop.orders.services.email

private[services] class EmailService {...}

Then in a different package, I use that class:

package shop.ui

import shop.orders.services.email.EmailService

class PaymentConfirmation extends WithFacesContext {

    var emailService: EmailService = null

Looking at the generated bytecode, there is no sign of any access modifier, which makes sense, as Java does not support such access restrictions. So what happens if I create a library containing code like block one, and attempt to compile block two against the library - there is no chance that the compiler will fail, since the information is lost. Or is it contained in something like a manifest?

I'm using Scala 2.9.2.

like image 228
John Smith Avatar asked Dec 04 '12 21:12

John Smith


People also ask

What is protected access modifier in Scala?

Scala Example: Protected Access Modifier Protected access modifier is accessible only within class, sub class and companion object. Data members declared as protected are inherited in subclass.

What is the default access level to fields in Scala objects?

3. Public: There is no public keyword in Scala. The default access level (when no modifier is specified) corresponds to Java's public access level. We can access these anywhere.

What is protected in Scala?

Protected Scope Scala protected is different from protected in java. To mark a member protected, use the keyword protected before a class or variable. Protected members can be accessed only by the sub classes in the same package.

What is the default access modifier in Scala?

If a class member is public, no access modifier is required in the definition. In fact, Scala doesn't have a public keyword. This is the default access if we don't specify any modifier, and is equivalent to the Java public access.


1 Answers

You could reference EmailService from Java, but not from Scala, because Scala stores the signature of the class as a scala.reflect.ScalaSignature annotation. The Scala compiler will fail with the following error:

class EmailService in package email cannot be accessed in package shop.orders.services.email

like image 151
yakshaver Avatar answered Oct 12 '22 13:10

yakshaver