将具有类型参数的类传递/返回给函数 [英] Passing/returning a class with type parameter to a function

查看:88
本文介绍了将具有类型参数的类传递/返回给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public static< T>假设以下方法(从Guava的Iterables说)可迭代< T>过滤器(final Iterable<>未过滤,final Class< T>类型){
return null;
}

以及此集合:

 组<?> objs = ...; 

然后下面的代码编译并且泛型被正确地派生出来

  Iterable< String> a2 = Iterables.filter(objs,String.class); (在Guava中,这将返回 objs 中所有字符串的可迭代字符串。 $ b> 


$ b

但现在让我们假设以下类:

 静态类别Abc< E> {
E someField;
}

我不知道如何调用 filter code>并获得 Iterable< Abc<>>

 可迭代<美国广播公司> a3 = Iterables.filter(objs,Abc.class); 
Iterable< Abc<>> a4 = Iterables.filter(objs,Abc.class); //编译错误 - Abc和Abc<?>是不兼容的类型
可重用< Abc<>>> a5 = Iterables.filter(objs,Abc<> .class); //编译错误
Iterable< Abc<>>> a6 =可计算< Abc>>过滤器(objs,Abc.class); //编译错误
Iterable< Abc<>>> a7 =(Iterable< Abc<>)Iterables.filter(objs,Abc.class); //编译错误 - 不可转换的类型
Iterable< Abc<>>> a8 = Iterables.filter(objs,new Abc<>()。getClass()); //编译错误
Iterable< Abc<>>> a8a = Iterables.filter(objs,new Abc< Object>()。getClass()); //编译错误

只有a3编译,但是我没有Abc参数,因此没有在后续代码中进行泛型类型检查。



我知道类型参数在运行时不存在,所以我不试图编写如下代码:

  Iterable< Abc< String>> a9 = Iterables.filter(objs,Abc< String> .class); //编译错误

我只想过滤Abc类型的所有对象(如a3)在结果中具有通用参数。我发现这样做的唯一方法是以下,这很愚蠢:

  Iterable< Abc<>> a10 = new HashSet< Abc<>>(); (abc<> a:Iterables.filter(objs,Abc.class)){
((Set }

谢谢。 b $ b

解决方案

这个问题没有令人满意的答案。类型参数化的类型只能使用无界通配符,理论上可行,我们只是没有。



您可以生成 Class< Abc<>>>< / code>类型对象,并将其移到实用方法或字段中。只要 Abc s很少,这个效果很好。

  @SuppressWarnings(unchecked)
public static Class< Abc<?>> ABC =(Class


Let's assume the following method (say from Guava's Iterables):

public static <T> Iterable<T> filter(final Iterable<?> unfiltered, final Class<T> type) {
    return null;
}

and this collection:

Set<?> objs = ...;

then the following code compiles and the generics are correctly derived

Iterable<String> a2 = Iterables.filter(objs, String.class);

(In Guava this would return an iterable of all Strings in objs.)

But now lets assume the following class:

static class Abc<E> {
    E someField;
}

I have no idea how to call filter and get Iterable<Abc<?>>:

Iterable<Abc>    a3 = Iterables.filter(objs, Abc.class);
Iterable<Abc<?>> a4 = Iterables.filter(objs, Abc.class); // Compile error - Abc and Abc<?> are incompatible types
Iterable<Abc<?>> a5 = Iterables.filter(objs, Abc<?>.class); // Compile error
Iterable<Abc<?>> a6 = Iterables.<Abc<?>>filter(objs, Abc.class); // Compile error
Iterable<Abc<?>> a7 = (Iterable<Abc<?>>) Iterables.filter(objs, Abc.class); //  Compile error - inconvertible types
Iterable<Abc<?>> a8 = Iterables.filter(objs, new Abc<?>().getClass()); // Compile error
Iterable<Abc<?>> a8a = Iterables.filter(objs, new Abc<Object>().getClass()); // Compile error

Only a3 compiles, but then I do not have the parameter on Abc, and thus no generic type checking in subsequent code is done.

I know that the type parameters are not present at runtime and so I do not atempt to write code like:

Iterable<Abc<String>> a9 = Iterables.filter(objs, Abc<String>.class); // Compile error

I just want to filter all objects of the type Abc (as a3 does) but having the generic parameter in the result. The only way of doing this I found is the following, which is silly:

Iterable<Abc<?>> a10 = new HashSet<Abc<?>>();
for (Abc<?> a : Iterables.filter(objs, Abc.class)) {
    ((Set<Abc<?>>)a10).add(a);
}

Thanks.

解决方案

There is no satisfactory answer to this question. Class literals for types parameterized with unbounded wildcards only would work out in theory, we just don't have them.

You can produce the Class<Abc<?>>-typed class object with an unchecked cast and move it to a utility method or field. As long as there are few Abcs, this works quite nicely.

@SuppressWarnings("unchecked")
public static Class<Abc<?>> ABC = (Class<Abc<?>>)(Object) Abc.class;

这篇关于将具有类型参数的类传递/返回给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆