Creating a more specific implicit use of a structural type with Scala

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 =>
   //do something
  }
}

I want to use it as follows:

prop map { actualX =>
  //do something
}

I have a simplified example. First the various parts that I use:

// leaving out the observable part
trait ObservableValue[T] {
  def value: T
}

trait LowerPriorityImplicits {
  // default implementation that adds a regular map method
  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 {

  // describe a type that has a map method
  type HasMapMethod[A, Container[X]] = {
    def map[B](f: A => B): Container[B]
  }

  // complex implementation that uses the builtin map if present
  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 =>
  // this fails because the compiler finds the lower priority implicit
  (value: Int).toString
}

// the method itself works fine
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.

+5
1

- , . , :

T[Dummy <: Container[_]] <: ObservableValue[Dummy]

:

object ObservableValue {

  implicit class Mappable[A](o: ObservableValue[A]) {

    def map[B](f: A => B): ObservableValue[B] = new ObservableValue[B] {
      def value = f(o.value)
    }
  }

  // FMC = FilterMonadicContainer
  // D = Dummy
  implicit class FilterMonadicMappable[A, FMC[D] <: FilterMonadic[D, FMC[D]]](o: ObservableValue[FMC[A]]) {

    def map[B, That](f: A => B)(implicit bf: CanBuildFrom[FMC[A], B, That]): ObservableValue[That] = new ObservableValue[That] {
      def value = o.value.map(f)
    }
  }

  type HasMap[A, That[_]] = {
    def map[B](f: A => B): That[B]
  }

  // OVC = ObservableValueContainer
  // MC = MappableContainer
  // D = Dummy
  implicit class SimpleMappable[A, MC[D] <: HasMap[D, MC], OVC[D <: MC[_]] <: ObservableValue[D]](o: OVC[MC[A]]) {

    def map[B](f: A => B): ObservableValue[MC[B]] = new ObservableValue[MC[B]] {
      def value = o.value.map(f)
    }
  }

}
0

All Articles