what does this extra private[class]() in scala class definition mean? - scala

I'm reading over someone elses code on Scala in order to learn the language a little better, but I'm stumped at what the following means "privateutil" If I just saw the [util] I would suspect that it was some sort of specific generic? but its got it's own private modifier?
class RPGPluginProperties private[util]() extends Properties {

From http://www.scala-lang.org/files/archive/spec/2.11/05-classes-and-objects.html#private:
A private modifier can be qualified with an identifier C (e.g.
private[C]) that must denote a class or package enclosing the
definition. Members labeled with such a modifier are accessible
respectively only from code inside the package C or only from code
inside the class C and its companion module.
So in this case the private modifier is making the no-args constructor private to the util class/package.
To declare the class private to the scope util, it would be private[util] class RPGPluginProperties...

Related

What do access modifiers at class/object/trait level mean?

In scala I can add access modifiers to a class/trait/object, like
private class Foo
protected[this] trait Foo
I have not found any good explanation on these class/trait/object-level modifiers. Do all such combinations make sense and what do they actually mean?
They mean the same as access modifiers for class/trait members, as classes and traits can also be members of other classes. For example:
class A {
private class Foo
}
Class Foo is only visible to class A. If I change the modifier to private[this], then it is called object private, and so any Foo is only visible to it's parent instance of A.
Declaring private, private[this], protected or protected[this] only really makes sense within another class or trait, because it has to be private to something. In this case, Foo being private to A. The same applies to traits.
We could also not have a containing object, and make them package private.
package com.example.foo
private[foo] class Foo
Now class Foo is only visible to other members of the package com.example.foo.
Do they make sense? In some cases I'm sure it is useful to have private classes and traits within some other object.

Typescript- Using class as interface, why do I have to implement private members/methods?

I'm using the Mixin pattern as illustrated below. Why does Typescript require you to provide stand-in properties for private properties of the mixin class in the target class (A)? It perfectly makes sense for the public properties, but for private properties it unnecessarily crufts up the target class with details of the internal implementation of the mixin class by requiring them to be stubbed-out in the target class. Seems like the Typescript transpiler should be able to not require this.
class Mixin {
private foo:string;
}
class A implements Mixin {
// stand-in properties, typescript requires even
// private properties to be stubbed-out
foo:string;
}
Private members contribute to the structure of a type in TypeScript, so if you don't implement them you are not compatible with the type. This actually makes it impossible to match a type structurally in TypeScript if it has a private member, because you are either:
a. Failing to provide the type
or
b. Providing a separate implementation of the private member
So you can only extend a type with a private member, not implement it.
With this in mind, you are better off not using private members with mixins. Provide the ghost-members in the implementation class and keep your fingers crossed that if mixins gain some traction the ghosting will become unnecessary (see TypeScript mixins part one).

Scala parameters for access modifiers?

What is the difference between
class Test {
private[this] val foo = 0
}
vs
class Test {
private val foo = 0
}
What all can go inside the []? Also, what should I search for when I want to look up the specs of this? I tried Googling various combinations of "scala access modifier arguments/parametrized scala access modifier" and nothing came up.
what should I search for when I want to look up the specs of this?
In The Scala Language Specification it is defined as "access modifier" and "access qualifier" (see BNF in §5.2).
What is the difference between
...
What all can go inside the []?
You can put class name, package name or this there. Here is a relevant quote from language specs that explains this (see §5.2 for more details):
The modifier can be qualified with an identifier C (e.g. private[C ]) that must
denote a class or package enclosing the definition. Members labeled with
such a modifier are accessible respectively only from code inside the package
C or only from code inside the class C and its companion module (§5.4).
An different form of qualification is private[this]. A member M marked
with this modifier is called object-protected; it can be accessed only from
within the object in which it is defined. That is, a selection p.M is only legal if the prefix is this or O.this, for some class O enclosing the reference. In
addition, the restrictions for unqualified private apply.
The first one is private for instance class, second is for class. If you use second version you have access from another instance of Test class (it's usefull for equals method or similiar).

Private scoping with square brackets (private[...]) in Scala

I've come across the following syntax while looking through the Gatling source code:
private[http] def build = {
// ...
}
What is the syntax inside square brackets?
When I click through it in my IDE it is an alias to a fully qualified package (com.excilys.ebi.gatling.http) but I can't find where that alias was defined.
See the scala reference, specifically, chapter 5.2. Some excerpt:
The private modifier can be used with any definition or declaration in a template. Such members can be accessed only from within the directly enclosing template and its companion module or companion class (§5.4). They are
not inherited by subclasses and they may not override definitions in parent
classes.
The modifier can be qualified with an identifier C (e.g. private[C]) that must
denote a class or package enclosing the definition. Members labeled with
such a modifier are accessible respectively only from code inside the package C or only from code inside the class C and its companion module (§5.4).
Such members are also inherited only from templates inside C.
In short: this is used for scope protection:
private[C] means that access is private "up to" C, where C is the
corresponding package, class or singleton object.
Same to protected[C]
protected[C]: access is protected "up to" C, where C is the
corresponding package, class or singleton object.

How do you access variables of super class of passed class in Groovy Script Engine?

I'm dynamically running groovy scripts from scala. And there are some instances of some class passed to groovy scripts via setProperty(). For example, say you have a class named TestA and class TestB inherits class TestA. And you are passing an instance of class B to groovy script like this
setProperty("testB", testB) // testB is an instance of class TestB
and running the groovy script, I can access variables declared in TestB. but when I try to access variables of TestA, which is super class of TestB, the groovy gives an error saying " No such property for class".
I can still call methods of both TestA and TestB from the given instance. So if you just write setter and getter, I can access to TestA's variables but I don't want to do it.
Is there anyway to access TestA's variables without using setter/getter? like using Expando or something?
Since you don't want to write the getters/setters yourself (which would be ugly boilerplate in Scala), you can simply add the scala.reflect.BeanProperty annotation (or scala.reflect.BooleanBeanProperty) to any fields you'd like to access from another JVM language. This will give you a more accessible API.
#scala.reflect.BeanProperty // generates getStatus() and setStatus() methods
var status = ""
Actually, after giving it more thought, you could simply access the accessor methods that Scala generates for itself. You may be able to access Scala's accessor methods from Groovy, but accessing the setter variable_$eq may look a bit weird.
[dlee#dlee-mac scala]$ cat Prop.scala
class Prop {
var variable = "foo"
val constant = "bar"
}
[dlee#dlee-mac scala]$ scalac Prop.scala
[dlee#dlee-mac scala]$ javap Prop
Compiled from "Prop.scala"
public class Prop extends java.lang.Object implements scala.ScalaObject{
public java.lang.String variable();
public void variable_$eq(java.lang.String);
public java.lang.String constant();
public Prop();
}