Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

private[this] vs private

Tags:

scala

In Scala I see such feature as object-private variable. From my not very rich Java background I learnt to close everything (make it private) and open (provide accessors) if necessary. Scala introduces even more strict access modifier. Should I always use it by default? Or should I use it only in some specific cases where I need to explicitly restrict changing field value even for objects of the same class? In other words how should I choose between

class Dummy {     private var name = "default name" }  class Dummy {     private[this] var name = "default name" } 

The second is more strict and I like it but should I always use it or only if I have a strong reason?

EDITED: As I see here private[this] is just some subcase and instead of this I can use other modifiers: "package, class or singleton object". So I'll leave it for some special case.

like image 372
Soteric Avatar asked Mar 14 '12 09:03

Soteric


People also ask

What is private this 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.

When should method be private?

Private methods are typically used when several methods need to do the exact same work as part of their responsibility (like notifying external observers that the object has changed), or when a method is split in smaller steps for readability.

When should a method be private vs public?

Which one we should use? We should use public access modifier if we want to make the method or property visible from anywhere, other classes, and instances of the object. Use the private access modifier if you want to make the method or property visible in its own class only.

What is the difference between public and private keyword?

public means you can access it anywhere while private means you can only access it inside its own class. Just to note all private, protected, or public modifiers are not applicable to local variables in Java. a local variable can only be final in java.


2 Answers

There is a case where private[this] is required to make code compile. This has to do with an interaction of variance notation and mutable variables. Consider the following (useless) class:

class Holder[+T] (initialValue: Option[T]) {     // without [this] it will not compile     private[this] var value = initialValue      def getValue = value     def makeEmpty { value = None } } 

So this class is designed to hold an optional value, return it as an option and enable the user to call makeEmpty to clear the value (hence the var). As stated, this is useless except to demonstrate the point.

If you try compiling this code with private instead of private[this] it will fail with the following error message:

error: covariant type T occurs in contravariant position in type Option[T] of value value_= class Holder[+T] (initialValue: Option[T]) {

This error occurs because value is a mutable variable on the covariant type T (+T) which is normally a problem unless marked as private to the instance with private[this]. The compiler has special handling in its variance checking to handle this special case.

So it's esoteric but there is a case where private[this] is required over private.

like image 175
denis phillips Avatar answered Sep 17 '22 20:09

denis phillips


I don't think it matters too much, since any changes will only touch one class either way. So the most important reason to prefer private over protected over public doesn't apply.

Use private[this] where performance really matters (since you'll get direct field access instead of methods this way). Otherwise, just settle on one style so people don't need to figure out why this property is private and that one is private[this].

like image 22
Alexey Romanov Avatar answered Sep 20 '22 20:09

Alexey Romanov