1

my project is mixed java and scala language, but some type mismatch error occur and I think it's a common problem in term of java and scala communicate. I organized the stage with simple classes. Environment is java 1.8 and scala 2.11.7

class Item[+T](name: String)

//ready use Item as MM type
class Packet[+MM[_]]

object GenS extends App {
  //use Item class
  def doWithPacket(packet: Packet[Item]) = {}

  //type error occur on packetFormJava variable form java
  val packetFormJava = GetGenJ.newPacketInJava
  doWithPacket(packetFormJava)

  //run well
  val packetFromScala = new Packet[Item]
  doWithPacket(packetFromScala)
}

and the java class simple as this:

public class GetGenJ {
    public static Packet<Item> newPacketInJava() {
        return new Packet<Item>();
    }
}

The compile error encounter:

Error:(16, 16) type mismatch;  
 found   : Packet[Item[_]]  
 required: Packet[Item]  
  doWithPacket(packetFormJava)  
                  ^

any help or advice thanks.

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
LoranceChen
  • 2,453
  • 2
  • 22
  • 48
  • In other word, how does the java represent scala's Packet[Item[_]] type? – LoranceChen Dec 31 '15 at 06:53
  • Is adding a utility method in Scala an option? That might be the easiest solution. I'm not sure you can represent higher kinded types in Java at all. – 0__ Dec 31 '15 at 14:01
  • hi @0__ ,Item[ _ ] should be represent a higher kind type in Java. It seem I might avoid Java code return a higher kind type.thanks anyway – LoranceChen Dec 31 '15 at 15:57

1 Answers1

1

In other word, how does the java represent scala's Packet[Item[_]] type?

It doesn't. Java simply doesn't have higher-kinded types. The information that MM is higher-kinded is hidden inside @ScalaSignature.

When you write Packet<Item>, you are using a raw type (which, in turn, can't be represented in Scala); you couldn't use Item<Something> inside newPacketInJava.

Community
  • 1
  • 1
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • thanks, I will avoid do the high kinder types in java.The discuss about raw type seems it was a legacy problem.happy new year~ – LoranceChen Jan 01 '16 at 01:11