相关疑难解决方法(0)

为什么不能在有界通配符通用中有多个接口?

我知道Java的泛型类型存在各种反直觉属性.这是我不理解的一个,我希望有人可以向我解释.为类或接口指定类型参数时,可以绑定它以使其必须实现多个接口public class Foo<T extends InterfaceA & InterfaceB>.但是,如果您要实例化实际对象,则此功能不再起作用.List<? extends InterfaceA>很好,但List<? extends InterfaceA & InterfaceB>无法编译.请考虑以下完整代码段:

import java.util.List;

public class Test {

  static interface A {
    public int getSomething();
  }

  static interface B {
    public int getSomethingElse();
  }

  static class AandB implements A, B {
    public int getSomething() { return 1; }
    public int getSomethingElse() { return 2; }
  }

  // Notice the multiple bounds here. This works.
  static class AandBList<T extends A & B> {
    List<T> …
Run Code Online (Sandbox Code Playgroud)

java generics language-design bounded-wildcard

69
推荐指数
3
解决办法
1万
查看次数

在Haskell中,在类A类的函数中从另一个类型B返回一些东西

我正在做一个有趣的项目,我正在尝试从Java重做一些基本数据类型和概念.目前我正在处理迭代器.

我的方法如下:(1)将接口转换为类型类(2)为实际实现声明自定义数据类型和实例

所以我创建了以下类型类:

class Iterator it where
    next :: it e -> (it e, e)
    hasNext :: it e -> Bool

class Iterable i where
    iterator :: Iterator it => i e -> it e

class Iterable c => Collection c where
    add :: c e -> e -> c e
Run Code Online (Sandbox Code Playgroud)

是的,我正在尝试翻译迭代器的概念(在这种情况下,它只是一个围绕实际列表的框).

这是我对一个简单List的实现:

data LinkedList e = Element e (LinkedList e) | Nil
    deriving (Show, Eq)

instance Collection LinkedList where
    add Nil e = Element e Nil
    add (Element x …
Run Code Online (Sandbox Code Playgroud)

java haskell types type-constraints

4
推荐指数
1
解决办法
251
查看次数