Higher-kinded generics in Java

后端 未结 5 939
别跟我提以往
别跟我提以往 2020-12-05 13:07

Suppose I have the following class:

public class FixExpr {
  Expr in;
}

Now I want to introduce a generic argument, abstract

5条回答
  •  无人及你
    2020-12-05 14:04

    In order to pass a type parameter, the type definition has to declare that it accepts one (it has to be generic). Apparently, your F is not a generic type.

    UPDATE: The line

    F> in;
    

    declares a variable of type F which accepts a type parameter, the value of which is Fix, which itself accepts a type parameter, the value of which is F. F isn't even defined in your example. I think you may want

    Fix in;
    

    That will give you a variable of type Fix (the type you did define in your example) to which you are passing a type parameter with value F. Since Fix is defined to accept a type parameter, this works.

    UPDATE 2: Reread your title, and now I think you might be trying to do something similar to the approach presented in "Towards Equal Rights for Higher-Kinded Types" (PDF alert). If so, Java doesn't support that, but you might try Scala.

提交回复
热议问题