Asq
04/02/2022, 2:28 PMW
in the example below) with more than one constraint, if a class delegates some of its implementation details?
e.g.
class DirectedEdgeWeighted<W> private constructor (val de: DirectedEdge, val w: W, val wAccretion: (W, W) -> W): DirEd by de where W: Any, W: Comparable<W> { ... }
produces syntax errors, even after trying several permutations in how the part after the constructor is declared, and before the beginning (say, data class
) too. It is important that DirectedEdgeWeighted
is-a DirEd
but not is-a DirectedEdge
so inheritance is not a solution. This issue is interesting on the basis of syntax principles, so please let's ignore the possibility of making inheritance feasible. I could hypothetically drop W: Any
as a workaround, yes, but a workaround is not the goal of the answer that I seek. Reassuming, without the example above: my question is:
is there a manner to use a type variable ("generic") with more than one constraint, if a class delegates some of its implementation details?
Thanks for your kind and analytical interest 🙂Youssef Shoaib [MOD]
04/03/2022, 11:25 AMwhere
on a separate line just works:
interface DirEd
class DirectedEdge: DirEd
class DirectedEdgeWeighted<W>
constructor (val de: DirectedEdge, val w: W, val wAccretion: (W, W) -> W): DirEd by de
where W: Any, W: Comparable<W> { }
fun main(){
println(DirectedEdgeWeighted(DirectedEdge(), 20, Int::plus))
}
Asq
04/04/2022, 6:45 PMYoussef Shoaib [MOD]
04/07/2022, 7:47 AMAsq
04/09/2022, 1:09 PM