4

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?

Ezhik
  • 876
  • 6
  • 23
  • 1
    I'm struggling to reproduce this. Could you provide a section of code that produces this issue in isolation? – James_pic May 17 '13 at 13:06

1 Answers1

1

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.

Community
  • 1
  • 1
som-snytt
  • 39,429
  • 2
  • 47
  • 129