Implementing methods having raw types in Scala

馋奶兔 提交于 2019-11-26 23:22:53

问题


I have a problem in using Drools Planner (written in Java) in Scala. One of interfaces in Drools planner is declared as:

public interface Score<S extends Score> extends Comparable<S>

However another interface uses 'Score' as a raw type:

public interface Solution {
    Score getScore();

Then I'd like to implement this interface in Scala:

class MySolution extends Solution {
    def getScore: Score = ...

And I get a compilation error: Scala compiler disallows writing just 'def getScore: Score'. When I try adding 'Score[_]' or 'Score[whatever]' compiler complains about type incompatibility. What should I do?


回答1:


The next release of Drools Planner (5.2.0.M2) will fix this issue. Here's the commit on git.

In some cases, people want to define their own Score implementation (for example NurseRosterScore implements HardAndSoftScore), to be able to show to the user per hard or soft constraint type what exactly is violated in the best solution. This change is the first step to make that easier and cleaner (even though it's already possible).




回答2:


Write a Java class to serve as a bridge between what the Java interface requires and what Scala allows.

SolutionBridge.java:

abstract class SolutionBridge implements Solution {
    public Score getScore() {
        return scalaGetScore();
    }

    abstract Score<?> scalaGetScore();
}

SolutionScala.scala:

class SolutionScala extends SolutionBridge {
    def scalaGetScore() = null.asInstanceOf[Score[_]]
}



回答3:


The error message which you omitted is not an extraneous detail. Re "I would also expect some support from Scala on this", you might also opt to participate in the process by reading the error messages and, if you don't understand them, include them when asking a question rather than vaguely paraphrasing them.

Error messages, they're important. Even when they're confusing. Especially when they're confusing.

Here is the error in 2.8.1:

a.scala:2: error: overriding method getScore in trait Solution of type ()Score[_ <: Score];
 method getScore has incompatible type
  def getScore: Score[_] = null
      ^
one error found

Here is the error with trunk:

a.scala:2: error: overriding method getScore in trait Solution of type ()Score[_ <: AnyRef];
 method getScore has incompatible type
  def getScore: Score[_] = null
      ^
one error found

There is a key difference there, which contributes to why it works with trunk when I do as the error message instructs me.

// this compiles with 2.9, but not with 2.8
class MySolution extends Solution {
  def getScore: Score[_ <: AnyRef] = null
}

The way the raw type Score is being used in the java source (as a type constructor in one location but with an implied existential type argument in another, with the second appearance bounding the first) it's a wonder it works anywhere. You don't want to know how much damage accomodating this sort of thing has already inflicted on the compiler. It's true that it would be nice if raw types just worked, but many things would be nice. Some things are not possible, some things are not desirable, and some things require too much effort from the tiny number of people keeping the ship afloat. Raw types win the triple crown.




回答4:


Have you tried to cast Score:

val s = solution.getScore.asInstanceOf[Score[Int]]


来源:https://stackoverflow.com/questions/5279149/implementing-methods-having-raw-types-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!