avanwieringen
avanwieringen

Reputation: 2444

Equivalent Scala code of Java code not working (method signature not valid)

I was working with the JUNG2 library such that I can visualize a neural network system I have created.

First I was following the code samples from JUNG2 and I am using the following Java code:

DirectedSparseMultigraph<Integer, Integer> graph = new DirectedSparseMultigraph<Integer, Integer>();        
DelegateForest<Integer, Integer> delf = new DelegateForest<Integer, Integer>();
Factory<Tree<Integer, Integer>> delt = DelegateTree.<Integer, Integer>getFactory(); 
Transformer<Integer, Double> trans = new Transformer<Integer, Double>() {
    @Override
    public Double transform(Integer arg0) {
        return 1.0;
    }
};      
MinimumSpanningForest2<Integer, Integer> prim = new MinimumSpanningForest2<Integer, Integer>(graph, delf, delt, trans);

and now the equivalent code in Scala:

var graph = new DirectedSparseMultigraph[Int, Int]
var delf = new DelegateForest[Int, Int]()
var delt = DelegateTree.getFactory[Int, Int]()
var trans = new Transformer[Int, Double] {
  def transform(input:Int):Double = {
    return 1.0
  }
}  
var prim:MinimumSpanningForest2[Int, Int] = new MinimumSpanningForest2(graph, delf, delt, trans)

In the Scala example the instantiation of MinimumSpanningForest2 gives me a long error in Eclipse saying:

overloaded method constructor MinimumSpanningForest2 with alternatives: 
 (edu.uci.ics.jung.graph.Graph[V,E],edu.uci.ics.jung.graph.Forest[V,E],org.apache.commons.collections15.Factory[_ <: 
 edu.uci.ics.jung.graph.Graph[V,E]],org.apache.commons.collections15.Transformer[E,java.lang.Double])edu.uci.ics.jung.algorithms.shortestpath
 .MinimumSpanningForest2[V,E] <and> 
 (edu.uci.ics.jung.graph.Graph[V,E],org.apache.commons.collections15.Factory[edu.uci.ics.jung.graph.Forest[V,E]],org.apache.commons.collecti
 ons15.Factory[_ <: 
 edu.uci.ics.jung.graph.Graph[V,E]],org.apache.commons.collections15.Transformer[E,java.lang.Double])edu.uci.ics.jung.algorithms.shortestpath
 .MinimumSpanningForest2[V,E] cannot be applied to (edu.uci.ics.jung.graph.DirectedSparseMultigraph[Int,Int], 
 edu.uci.ics.jung.graph.DelegateForest[Int,Int], org.apache.commons.collections15.Factory[edu.uci.ics.jung.graph.Tree[Int,Int]], java.lang.Object 
 with org.apache.commons.collections15.Transformer[Int,scala.Double])

I have been looking for ages but am unable to see a problem. Looking at the API docs of Collections and JUNG2 I am positive that the generics are correct. The JAVA example is working flawlessly.

Upvotes: 1

Views: 304

Answers (1)

avanwieringen
avanwieringen

Reputation: 2444

Ugh... incredible. As I posted the question after 2 hours of tryig I found my answer myself. This is probably the so-called door-knob effect....

When typing Double in a .scala file this becomes scala.lang.Double (ofcourse) whereas my method above needs java.lang.Double. So it is solved by altering it to this:

var transformer = new Transformer[Int, java.lang.Double] {   
  def transform(input:Int):java.lang.Double = {
    return 1.0
  }   
}

Although Java and Scala are interchangeable you have to be very careful with the Java and Scala identically named classes. If you would program Scala without prior Java knowledge this problem would be very hard to solve probably.

Upvotes: 7

Related Questions