泛型类型上可能有多个类型包含吗? [英] Is it possible to have multiple type constaints on a generic type?

查看:72
本文介绍了泛型类型上可能有多个类型包含吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在飞镖中,您可以

class Preference<T extends int>

定义类型约束.但是,有没有一种方法可以定义多个约束?

to define a type constraint. But is there a way to define multiple constrains?

我尝试过

class Preference<T extends int, String>

但是当我尝试将T类型的参数传递给除String语句之外的函数时,会引发错误

But an error get's thrown when I try to pass a argument of type T to a function that excepts a String saying

不能将参数类型"T"分配给参数类型"String"

The argument type 'T' can't be assigned to the parameter type 'String'

推荐答案

否,Dart类型参数只能具有单个约束.没有解决方法.

No, Dart type parameters can only have a single constraint. There is no workaround.

这篇关于泛型类型上可能有多个类型包含吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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