Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Java equivalent of this Scala code?

Tags:

java

scala

What is the Java equivalent of these traits in Scala?

trait Visitor {
  type X
  type S<:Strategy
  type R[v<:Visitor] = (S{type X = Visitor.this.X;type V=v})#Y
}

trait Strategy {
  type V<:Visitor
  type X
  type Y
}

I translate the Strategy trait to:

public interface Strategy<V extends Visitor<?, ?, ?>, X, Y> {

}

I try translating trait Visitor to:

public interface Visitor<X, S extends Strategy<?,?, ?>, R ?????> {
}

As you can see, I don't know how to understand/translate type R in the Visitor trait. What is a similar Java equivalent?

like image 468
Stuck Avatar asked Nov 01 '22 01:11

Stuck


1 Answers

I'm pretty sure that it is impossible to write an equivalent in Java, its type system is just not sophisticated enough. R[v <: Visitor] is a higher-kinded generic type and it would require something along these lines:

interface Visitor<X, S extends Strategy<?, ?, ?>, R<? extends Visitor<?, ?, ?>> extends ...>

but this is not possible to express in Java because it does not have higher-kinded types in generics. And that's not even mentioning that (S{type X = Visitor.this.X;type V=v})#Y bit which is a structural type with refinement (as far as I remember, it is called like this). I don't know any other language except Scala which has such thing.

like image 170
Vladimir Matveev Avatar answered Nov 04 '22 08:11

Vladimir Matveev