java 高级泛型,Java中更高级的泛型

Suppose I have the following class:

public class FixExpr {

Expr in;

}

Now I want to introduce a generic argument, abstracting over the use of Expr:

public class Fix {

F> in;

}

But Eclipse doesn't like this:

The type F is not generic; it cannot be parametrized with arguments >

Is this possible at all or have I overlooked something that causes this specific instance to break?

Some background information: in Haskell this is a common way to write generic functions; I'm trying to port this to Java. The type argument F in the example above has kind * -> * instead of the usual kind *. In Haskell it looks like this:

newtype Fix f = In { out :: f (Fix f) }

解决方案

I think what you're trying to do is simply not supported by Java generics. The simpler case of

public class Foo {

public T bar() { return null; }

}

also does not compile using javac.

Since Java does not know at compile-time what T is, it can't guarantee that T is at all meaningful. For example if you created a Foo, bar would have the signature

public BufferedImage bar()

which is nonsensical. Since there is no mechanism to force you to only instantiate Foos with generic Ts, it refuses to compile.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值