user1590636
user1590636

Reputation: 1194

limit T in a generic class to types that implement some interface

In dart consider the following

class Question<T>{
  String body;
  T answer;
}

//an interface
class IAnswer{
  String Value()=>this.toString();  
}

I need T to be limited to types the for example implement the interface IAnswer, is that possible without having to check if T is IAnswer and throw and exception in the Question constructor?

Upvotes: 10

Views: 1198

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657198

class Question<T extends IAnswer>{
  String body;
  T answer;
}

Upvotes: 7

Related Questions