Is it possible to have multiple type constaints on a generic type?

只愿长相守 提交于 2021-01-06 04:29:08

问题


In dart you can do

class Preference<T extends int>

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

I tried

class Preference<T extends int, 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

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


回答1:


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



来源:https://stackoverflow.com/questions/61820181/is-it-possible-to-have-multiple-type-constaints-on-a-generic-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!