Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax: multiple generic constraints and inheritance, interfaces

what is the correct syntax for specifying multiple generic bounds/constraints in Kotlin?

class CustomClass<T> where T: Constraint1, T: Constraint2,
    ParentClass<T>(), Interface1 { /* ... */ }

here Constraint1 and Constraint2 are unrelated constraints/bounds on T (eg: disjoint interfaces that T implements) and ParentClass is a generic (base) class also. Interface1 is an interface CustomClass will satisfy

like image 682
xst Avatar asked Apr 09 '26 20:04

xst


1 Answers

You need to specify the base class and the interfaces before the where clause:

class CustomClass<T>
    : ParentClass<T>(), Interface1
        where T : Constraint1, T : Constraint2 {
    /* ... */
}
like image 172
hotkey Avatar answered Apr 11 '26 14:04

hotkey