scala - Generically adding implicits to both TreeSet and TreeMaps -
i want add helpful implicits both mutable , immutable treemaps , treesets in scala.
here attempt:
first try define least upper bound of treemap , treeset has
headoption
/lastoption
(fromgentraversablelike
) ,from
/to
/until
(fromsorted
):type sortedcollection[a, repr <: sortedcollection[a, repr]] = collection.generic.sorted[a, repr] collection.gentraversablelike[a, repr]
write util:
implicit class richsortedcollection[a, repr <: sortedcollection[a, repr]](s: sortedcollection[a, repr]) { def greaterthanorequalto(a: a): option[a] = s.from(a).headoption def lessthan(a: a): option[a] = s.until(a).lastoption def lessthanorequalto(a: a): option[a] = s.to(a).lastoption }
this works partially: sortedset#greaterthan
compiles treemap#greaterthan
not. how fix it?
treemap[a, b]
(transitively) extends gentraversablelike[(a, b), treemap[a, b]]
, sorted[a, treemap[a, b]]
, it's a:
sorted[a, treemap[a, b]] gentraversablelike[(a, b), treemap[a, b]]
this close type alias, first type parameter of sorted
, gentraverablelike
in type alias sortedcollection
must same, not above. aren't compatible. is, repr = treemap[a, b]
fine, a = (a, b)
doesn't make sense.
you're going have same issue map types, , real choice re-implement richsortedcollection
maps well.
Comments
Post a Comment