Posts

Showing posts with the label higher-kinded-types

scala type class with higher kinded types and variance

scala type class with higher kinded types and variance I have a question very similar to this one: Scala higher kinded type variance This, however is slightly different in that, well, it doesn't compile (scala 2.11.8). The basic idea is to take a provided array of "things". If the array is null, return a default value of some type (e.g. Boolean , Option , List[Int] ), otherwise do work on the array and produce a result. The result and default value have the same type. Boolean Option List[Int] The challenge I'm having is getting this to work across a broad set of result types. Here's a contrived example: trait NullGuard[F[_]] { def nullGuard[A, B](arr: Array[A], default: F[B])(expr: => F[B]): F[B] = if (arr == null || arr.length == 0) default else expr } Let's create an implementation that returns an Option: implicit def optionNullGuard[F[X] <: Option[X]]: NullGuard[F] = new NullGuard[F]() {} The above does compile, but the following att...