As far as I know, the collection library does not have a common feature that defines the method map(most likely, because there are different signatures for map).
I have an observable value (I think the property in the ui system) has a change event. Observed values can be displayed using the method map.
If we are working with a type that already has a method map, we must use the built-in method map.
So, instead of:
prop map { x =>
x map { actualX =>
}
}
I want to use it as follows:
prop map { actualX =>
}
I have a simplified example. First the various parts that I use:
trait ObservableValue[T] {
def value: T
}
trait LowerPriorityImplicits {
implicit class RichObservableValue1[A](o: ObservableValue[A]) {
def map[B](f: A => B): ObservableValue[B] = new ObservableValue[B] {
def value = f(o.value)
}
}
}
object ObservableValue extends LowerPriorityImplicits {
type HasMapMethod[A, Container[X]] = {
def map[B](f: A => B): Container[B]
}
implicit class RichObservableValue2[A, Container[Z] <: HasMapMethod[Z, Container]](
o: ObservableValue[Container[A]]) {
def map[B](f: A => B): ObservableValue[Container[B]] =
new ObservableValue[Container[B]] {
def value = o.value.map(f)
}
}
}
- (, , ), , . :
class TestCase extends ObservableValue[Option[Int]] {
def value = None
}
val x = new TestCase
x map { value =>
(value: Int).toString
}
ObservableValue.RichObservableValue2(x) map { value =>
(value: Int).toString
}
Container[B] Any, RichObservableValue2.
, , .
, :
?
FilterMonadic . , map, Option.
2
, FilterMonadic . RichObservableValue3 RichObservableValue.
implicit class RichObservableValue3[A, C[Z] <: FilterMonadic[Z, C[Z]]](o: ObservableValue[C[A]]) {
def map[B, That](f: A => B)(implicit bf: CanBuildFrom[C[A], B, That]): ObservableValue[That] = new ObservableValue[That] {
def value = o.value.map(f)
val change = o.change map (_ map f)
}
}
, a List[Int] . - , implicits.