Failed to override java.lang.String field. What's wrong?

I tried to compile code containing

class FixedIndexedRepository(override val name: java.lang.String, location: URI) extends FixedIndexedRepo 

What extends FixedIndexedRepo , which extends Java class AbstractIndexedRepo

 public abstract class AbstractIndexedRepo implements RegistryPlugin, Plugin, RemoteRepositoryPlugin, IndexProvider, Repository { ... protected String name = this.getClass().getName(); ... 

Unfortunately, the 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 it? What's wrong?

+4
source share
1 answer

Rex says it's ugly:

Create an open accessor from an inherited secure Java field

Given:

 package j; public class HasName { protected String name = "name"; } 

then fake:

 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 warned about ugly.

I also did not quite understand the details, but the weekend is coming.

Unfortunately, Scala 2.9.2 compiler stops with an error

You mean, fortunately, it stops with an error.

+1
source

Source: https://habr.com/ru/post/1481367/


All Articles