飞镖多个上限 [英] Dart multiple upper bounds

查看:98
本文介绍了飞镖多个上限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用可实现3个接口的泛型来实现一个解决方案,但据我所知,dart中的泛型仅支持1个上限?

I need to implement a solution using generics that implements 3 interfaces, but as far as I can tell, generics in dart only supports 1 upper bound?

我有一个看起来像像这样:

I have a model that looks like this:

abstract class Category implements Built<Category, CategoryBuilder>, Identifiable, Mapable {
   ...
}

这3个接口的内容并不是很相关,我想做的是构造一个

The contents of the 3 interfaces is not really relevant, and what I'm trying to do, is construct a class that can process this in generic form.

我想要的是这样的东西:

What I want is something like this:

abstract class BaseDB<T extends Built<T, R> & Identifiable & Mapable, R extends Builder<T, R>> {
   process(T entity) {
      print(entity.id); // From Identifiable
      entity.toMap(); // From Mapable
      // ... etc
   }
}

I知道Typescript和Java都可以做到这一点,但是Dart我还很陌生。有人知道吗?

I know this is possible in both Typescript and Java, but I'm fairly new at Dart. Anyone know?

推荐答案

在Dart中是不可能的。您只能在类型变量上加上一个界限。

This is not possible in Dart. You can only put one bound on a type variable.

Dart类型变量的界限用于检查可以对类型参数类型的对象执行哪些操作。示例:

The bound of a Dart type variable is used to check which operations you can do on an object of the type parameter type. Example:

String something<T extends num>(T value) {
  return value.abs().toString();
}

您可以调用 abs()上的code>,因为我们知道的所有实例都是数字,而 num 具有 abs 方法。

You are allowed to call abs() on value because we know that all instances of value are numbers, and num has an abs method.

如果可以写< T扩展了Foo& Bar> ,则Dart类型系统中没有简单的类型可以描述类型为 T 的对象。 Dart没有交集类型(交集类型 Foo& Bar 将是所有均为<$ c $子类型的类型的超类型c> Foo Bar ,以及 Foo 和<$ c $的子类型c> Bar )。
如果 Foo 声明 Baz method() Bar 声明 Qux method(),并且 value 的类型为 T value.method()的类型是什么?
(可能是不允许的,或者类型是 Baz& Qux )。这表明在类型变量边界中允许& 会将交集类型泄漏到其余的类型系统中,并且由于Dart没有交集类型,因此它在类型上也没有多个边界

If you can write <T extends Foo & Bar>, then there is no simple type in the Dart type system that can describe objects of type T. Dart does not have intersection types (the intersection type Foo & Bar would be a supertype of all types that are subtypes of both Foo and Bar, and a subtype of both Foo and Bar). If Foo declares Baz method(), Bar declares Qux method(), and value has type T, what is the type of value.method()? (It would either be disallowed, or the type would be Baz & Qux). This shows that allowing & in type variable bounds leaks intersection types into the remaining type system, and since Dart does not have intersection types, it also does not have multiple bounds on type variables.

声明类时, FooBar 会同时实现两个 Foo Bar ,您会遇到相同的问题:您需要弄清楚方法会返回什么。但是,语言需要将您的解决方案到您的类中,以找到 FooBar.method ,因为否则 FooBar 类声明无效。它要求用户找到一种解决方案,以找到 Baz Qux 的子类。

When you declare a class, FooBar, implementing both Foo and Bar, you have the same issue: You need to figure out what method returns. However, the language requires you to write that solution into your class, to find some valid return type for FooBar.method, because otherwise the FooBar class declaration is not valid. It requires a user to find a solution to "find a subclass of both Baz and Qux".

这篇关于飞镖多个上限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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