带有泛型的Java F-Bound类型 [英] Java F-Bound types with generics

查看:193
本文介绍了带有泛型的Java F-Bound类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 有没有什么办法可以在java中表达f-边界类型,在调用位置,返回一个通用响应? >界面Functor< T延伸Functor< T>> 
public< B> T< B个映射(函数< A,B> fn); //不会编译,因为类型不匹配

我可以使用f-bound类型,如果类型永远不会改变,但在地图的情况下,我需要一个新的类型。有没有一种方法来表达这在Java?



我真正想要的是任何方式,我可以得到像更高类型的东西,即使我知道javac不支持更高的kinded类型。

假设我们有一个 List< A> 并且希望这个接口返回一个列表与LT; B个。但不希望这个接口知道关于 List 的任何信息。

解决方案

读取functor的维基百科定义,这听起来像是你想要定义一个能够从一个类别(Java类型)映射到另一个类型的泛型类型。在上例中,要从 List< A> 映射到 List< B> ,其中类型 A B 是通用的。



如果这是您的目标,然后考虑以下接口来定义 Functor 类型:

 公共界面Functor< CategoryA,CategoryB> {
公共CategoryB地图(CategoryA实例);

这声明 Functor 在两个通用参数类型中输入交易, CategoryA CategoryB ,并且这些参数类型没有限制。它还声明必须实现一个方法 map ,该方法将类型 CategoryA 的对象映射到类型 CategoryB



假设根据您的示例,您现在要创建<$ c $的具体实例c> Functor ,它从 List< Integer> 映射到 List< String> 。您可以创建以下类:

  public class IntegerListToStringList实现Functor< List< Integer>,List< String>> {
@Override
public List< String> map(List< Integer> integerList){
List< String> stringList = new ArrayList<>(integerList.size());
for(Integer intValue:integerList){
stringList.add(Integer.toString(intValue));
}
return stringList;


$ / code>

然后你可以调用这个具体的 Functor 执行如下:

  Functor< List< Integer>,List< String>> functor = new IntegerListToStringList(); 
Integer [] intArray = new Integer [] {2,3,5,7,11,13};
列表<整数> intList = Arrays.asList(intArray);
列表< String> stringList = functor.map(intList);
System.out.println(String list:+ stringList);

现在,任何期望 Functor< List< Integer>类型参数的方法;,List< String>> 可以接受类型为 IntegerListToStringList 的实例。

Is there any way to express f-bound types in java where at the call site, a generic response is returned?

interface Functor<T extends Functor<T>>
  public <B> T<B> map(Function<A, B> fn); // won't compile because types don't match

I can use f-bound types if the type never changes, but in the case of map, I need a new type. Is there a way to express this in java?

What I am really looking for is any way that I can get something like higher kinds even though I know javac doesn't support higher kinded types.

Lets say we have a List<A> and want this interface to return a List<B>. But don't want this interface to know anything about List.

解决方案

Reading the Wikipedia definition of functor, it sounds like you want to define a generic type which is able to map from one category (Java type) to another. In your example above, to map from a List<A> to List<B> where the types A and B are generic.

If this is your aim, then consider the following interface for the definition of a Functor type:

public interface Functor<CategoryA, CategoryB> {
    public CategoryB map(CategoryA instance);
}

This declares that the Functor type deals in two generic parameter types, CategoryA and CategoryB and no constraints are put on these parameter types. It also declares that a method map must be implemented which maps from an object of type CategoryA to an object of type CategoryB.

Suppose, based on your example, you now want to create a concrete instance of a Functor which maps from List<Integer> to List<String>. You might create the following class:

public class IntegerListToStringList implements Functor<List<Integer>, List<String>> {
    @Override
    public List<String> map(List<Integer> integerList) {
        List<String> stringList = new ArrayList<>(integerList.size());
        for(Integer intValue : integerList) {
            stringList.add(Integer.toString(intValue));
        }
        return stringList;
    }
}

You could then invoke this concrete Functor implementation like so:

Functor<List<Integer>, List<String>> functor = new IntegerListToStringList();
Integer[] intArray = new Integer[] {2, 3, 5, 7, 11, 13};
List<Integer> intList = Arrays.asList(intArray);
List<String> stringList = functor.map(intList);
System.out.println("String list: " + stringList);

Now any method which is expecting a parameter of type Functor<List<Integer>, List<String>> can accept an instance of type IntegerListToStringList.

这篇关于带有泛型的Java F-Bound类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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