## Replace View Bounds

Well the scala community has decided to deprecate view bounds in Scala. I suspect it might have to do with brevity. Full discussion can be viewed here

Well looks like most of the view bounds (<%) can be converted to context bounds. Lets see different strategies that can help us do the same. Suppose we have:

 1234567891011 scala> def foo[T <% Int](x: T):Int = xfoo: [T](x: T)(implicit evidence$1: T => Int)Int scala> implicit def a[T](n:T) = n match { | case x:String => x.toInt | }warning: there were 1 feature warning(s); re-run with -feature for detailsa: [T](n: T)Int scala> foo("23")res4: Int = 23 To convert it to context bound we need to implicitly have something that does T => Int. We could:  12345678 scala> type L[X] = X => Intdefined type alias L scala> def goo[T : L](x: T):Int = xgoo: [T](x: T)(implicit evidence$1: T => Int)Int scala> goo(23)res2: Int = 23

We could combine the type declaration with the function definition as:

 1234 scala> def goo[T : ({type L[X] = X => Int})#L](x: T):Int = xgoo: [T](x: T)(implicit evidence$1: T => Int)Int //quite cryptic though A more readable version:  12345 scala> def goo2[T](x: T)(implicit ev: T => Int):Int = xgoo2: [T](x: T)(implicit ev: T => Int)Int scala> goo2("12")res8: Int = 12 More generalized version:  1234567891011 scala> def goo[E,T : ({type L[X] = X => E})#L](x: T):E = xgoo: [E, T](x: T)(implicit evidence$1: T => E)E scala> def goo2[T, E](x: T)(implicit ev: T => E):E = xgoo2: [T, E](x: T)(implicit ev: T => E)E scala> goo("1000")res10: String = 1000 scala> goo2("1000")res11: String = 1000

Source