简体   繁体   中英

What is the Java equivalent of this Scala code?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM