I tried to compile code that contains
class FixedIndexedRepository(override val name: java.lang.String, location: URI) extends FixedIndexedRepo
Which extends FixedIndexedRepo which extends Java class AbstractIndexedRepo
public abstract class AbstractIndexedRepo implements RegistryPlugin, Plugin, RemoteRepositoryPlugin, IndexProvider, Repository {
...
protected String name = this.getClass().getName();
...
Unfortunately Scala 2.9.2 compiler stops with an error:
.../FixedIndexedRepository.scala:29: overriding variable name in class AbstractIndexedRepo of type java.lang.String;
[error] value name has incompatible type
[error] class FixedIndexedRepository(override val name: java.lang.String, location: URI) extends FixedIndexedRepo
How to fix this? What is wrong?
Rex says it is ugly:
Making a public accessor from an inherited protected Java field
Given:
package j;
public class HasName {
protected String name = "name";
}
then the fake-out is:
package user
private[user] class HasNameAdapter extends j.HasName {
protected def named: String = name
protected def named_=(s: String) { name = s }
}
class User(n: String = "nom") extends HasNameAdapter {
def name(): String = named
def name_=(s: String) { this named_= s }
this name_= n
}
object Test extends App {
val u = new User("bob")
Console println s"user ${u.name()}"
Console println s"user ${u.name}"
}
You were forewarned about the ugly.
I haven't quite worked out the details either, but the weekend is coming up.
Unfortunately Scala 2.9.2 compiler stops with an error
You mean, fortunately it stops with an error.
Related
I have play web application with conf/application.conf (nothing unusual). Guice is used for dependency injection. How can property value be injected in class constructor? The code is below.
class MyController #Inject() (private val foo: Foo) extends Controller {
...
}
#ImplementedBy(classOf[FooImpl])
trait Foo {
def bar: String
}
class FooImpl extends Foo {
override val bar = current.configuration.getString("my.bar").get
...
}
In the current configuration FooImpl can't be tested without running application. I want to be able instantiate FooImpl in unit tests. The perfect solution [from my point of view] should look like that:
class FooImpl #Inject() (#Named("my.bar") override val bar: String) extends Foo {
...
}
Unfortunately, this code doesn't work, because Guice doesn't have 'my.bar' binding:
No implementation for java.lang.String annotated with #com.google.inject.name.Named(value=my.bar) was bound.
The only solution that I came up with is writing my own module, which iterates through configuration properties and binds them as named dependencies (a variation of the example from this doc). But I believe that a better approach exists.
I implemented that using Java. I hope you can use it as reference for your Scala implementation.
At first, I created a Module:
public class MainModule extends AbstractModule {
public final static String TIMEOUT_IN_MILLISECONDS_ANNOTATION = "timeout-promise";
private final Configuration configuration;
public MainModule(#SuppressWarnings("unused") Environment environment, Configuration configuration) {
this.configuration = configuration;
}
#Override
protected void configure() {
long timeoutInMilliseconds = configuration.getLong("application.promise.timeout.in.milliseconds", 0L);
bindConstant().annotatedWith(Names.named(TIMEOUT_IN_MILLISECONDS_ANNOTATION)).to(timeoutInMilliseconds);
}
}
After that, I just used the annotation on different places:
class Service {
#Inject
#Named(MainModule.TIMEOUT_IN_MILLISECONDS_ANNOTATION)
protected long timeoutInMilliseconds;
}
Hope this helps.
Some time ago i was developed small guice extention for simple injection configuration variables mapped on Enum
guice-config
I encountered the same problem after about a year, and this time come up with the following solution (very similar to the one proposed by #stranger-in-the-q and #droidman):
class InjectionModule extends AbstractModule {
override def configure(): Unit = {
val config: Config = TypesafeConfigReader.config
config.entrySet().asScala.foreach { entry =>
val path = entry.getKey
entry.getValue.valueType() match {
case ConfigValueType.NUMBER =>
bind(classOf[Int])
.annotatedWith(Names.named(path))
.toInstance(config.getInt(path))
case ConfigValueType.BOOLEAN =>
bind(classOf[Boolean])
.annotatedWith(Names.named(path))
.toInstance(config.getBoolean(path))
case ConfigValueType.STRING =>
bind(classOf[String])
.annotatedWith(Names.named(path))
.toInstance(config.getString(path))
case _ =>
}
}
}
}
Also, this approach can be extended by appending prefixes to system properties (which key-value pairs are part of the loaded config):
private def getPrefix(configValue: ConfigValue): String = {
val description = configValue.origin().description()
if (description.contains("system properties")) {
"sys."
} else {
""
}
}
In this case instead of writing Names.named(path) one should use Names.named(getPrefix(entry.getValue) + path).
To inject multiple properties from a play configuration you could do this way. Create a map out of Play configuration and pass it as Properties to Guice binder.
public class Module extends AbstractModule {
private Environment environment;
private Configuration configuration;
public Module(Environment environment,Configuration configuration){
this.environment = environment;
this.configuration = configuration;
}
#Override
public void configure() {
Configuration helloConf = configuration.getConfig("myconfig");
Map<String, Object> map = helloConf.asMap();
Properties properties = new Properties();
properties.putAll(map);
Names.bindProperties(binder(), properties);
}
}
I see something like below sometimes.
class Test(val str: String){
}
I wrote some sample code with val though, I don't see any difference between common declaration and the way with val.
What is the difference between them?
and When should it be used?
If you add val, your variable will be visible from outside of class.
// 'name' is a val
class Person(val name: String)
val p = new Person("Alvin Alexander")
p.name // String = Alvin Alexander
p.name = "Fred Flintstone" // error: reassignment to val
// 'name' is neither var or val
class Person(name: String)
val p = new Person("Alvin Alexander")
p.name // error: value name is not a member of Person
The code was taken from http://alvinalexander.com/scala/scala-class-examples-constructors-case-classes-parameters
More info scala class constructor parameters
By adding to #EntryLevelDev's answer, you can use javap to see what really happened.
1.scala
class Test(val str: String){
}
step1. scalac 1.scala
step2. javap Test
Compiled from "1.scala"
public class Test {
public java.lang.String str();
public Test(java.lang.String);
}
2.scala :
class Test2(str: String){
}
step1. scalac 2.scala
step2. javap Test2
Compiled from "1.scala"
public class Test1 {
public Test1(java.lang.String);
}
So when you add val, scala generates a method by the argument's name, and is made public so is visible from outside.
Why can't I use this.getClass in auxiliary constructor in scala? Are there any alternatives?
More specifically, I am trying to call LoggerFactory.getLogger of slf4j in the auxiliary constructor. I have an hack now where I am forced to pass a logger object to the constructor.
A simple contrived example (does not compile) which shows what I am trying to do:
class A (numbers : Double) {
val logger = LoggerFactory.getLogger(this.getClass)
def this(numbersAsStr: String) = this (try { s.toDouble) } catch { case _ => LoggerFactory.getLogger(this.getClass).error("Failed to convert"); 0 }
}
This is actually a limitation of the JVM rather than specifically a Scala problem. Here's a similar example in Java:
public class ThisTest {
public final String name;
public ThisTest(String n) {
name = n;
}
public ThisTest() {
// trying to use `this` in a call to the primary constructor
this(this.getClass().getName());
}
}
When you try to compile it you get an error:
$ javac ThisTest.java
ThisTest.java:10: error: cannot reference this before supertype constructor has been called
this(this.getClass().getName());
^
1 error
The problem is that you're trying to reference this before this any of the super-constructors for this have been called. You will have the restriction that you can't use a this reference in a super() or this() call no matter what JVM language you use, because that's the way classes work on the JVM.
However, you can totally avoid this problem by restructuring your code to put the reference to this after the this() call:
class A (numbers: Double) {
val logger = LoggerFactory.getLogger(this.getClass)
def this(numbersAsStr: String) = {
this ( try { numbersAsStr.toDouble } catch { case _ => 0 } )
LoggerFactory.getLogger(this.getClass).error("Failed to convert");
}
}
You might actually want access to the thrown exception for your log info. In that case, I'd just use LoggerFactory.getLogger(classOf[A]). That won't give you the actual class name if you're using inheritance (which I was assuming was the case here), but if you include the stack trace in the log then you should be able to figure it out.
Not sure I understand the question. Here is a guess:
class Foo(val c: Class[_]) {
def this() = this(classOf[Foo])
}
new Foo().c // -> class Foo
When I migrated to Scala 2.9.0 from 2.8.1, all of the code was functional except for the Hadoop mappers. Because I had some wrapper objects in the way, I distilled down to the following example:
import org.apache.hadoop.mapreduce.{Mapper, Job}
object MyJob {
def main(args:Array[String]) {
val job = new Job(new Configuration())
job.setMapperClass(classOf[MyMapper])
}
}
class MyMapper extends Mapper[LongWritable,Text,Text,Text] {
override def map(key: LongWritable, value: Text, context: Mapper[LongWritable,Text,Text,Text]#Context) {
}
}
When I run this in 2.8.1, it runs quite well (and I have plenty of production code in 2.8.1. In 2.9.0 I get the following compilation error:
error: type mismatch;
found : java.lang.Class[MyMapper](classOf[MyMapper])
required: java.lang.Class[_ <: org.apache.hadoop.mapreduce.Mapper]
job.setMapperClass(classOf[MyMapper])
The failing call is when I call setMapperClass on the Job object. Here's the definition of that method:
public void setMapperClass(java.lang.Class<? extends org.apache.hadoop.mapreduce.Mapper> cls) throws java.lang.IllegalStateException { /* compiled code */ }
The definition of the Mapper class itself is this:
public class Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>
Does anyone have a sense of what I'm doing wrong? It looks to me like the type is fundamentally correct: MyMapper does extend Mapper, and the method wants something that extends Mapper. And it works great in 2.8.1...
Silly as it seems, you can work around the problem by defining the Mapper before the Job. The following compiles:
import org.apache.hadoop._
import org.apache.hadoop.io._
import org.apache.hadoop.conf._
import org.apache.hadoop.mapreduce._
class MyMapper extends Mapper[LongWritable,Text,Text,Text] {
override def map(key: LongWritable, value: Text, context: Mapper[LongWritable,Text,Text,Text]#Context) {
}
}
object MyJob {
def main(args:Array[String]) {
val job = new Job(new Configuration())
job.setMapperClass(classOf[MyMapper])
}
}
So in Java I had a class that contained a HashMap that used the class as a key pointing to an object of the same class.
class ComponentContainer {
private HashMap<Class<? extends Component>, Component> componentMap
public ComponentContainer {
componentMap = new HashMap<Class<? extends Component>, Component>();
}
public void set (Component c) {
componentMap.put(c.getClass(), c);
}
}
However when I try to do the same thing in Scala within a trait I find myself getting a type mismatch error that a java.lang.Class[?0] was found where Class[Component] was needed.
trait ComponentContainer {
val componentMap: HashMap[Class[Component], Component] = HashMap.empty
def set (c: Component) {
val t = (c.getClass, c)
componentMap += t
}
}
This has me absolutely stumped any help would be appreciated greatly.
The reason your code doesn't compile is that T.getClass method has result Class[_] and not Class[T]. Details of getClass has been explained by VonC here.
From your source code I cannot see if you care about type parameter of Class instances but following version of your code compiles:
trait ComponentContainer {
val componentMap: HashMap[Class[_], Component] = HashMap.empty
def set (c: Component) {
val t = (c.getClass, c)
componentMap += t
}
}