Last active
August 29, 2015 14:23
-
-
Save wheaties/ba1993e681f96b071655 to your computer and use it in GitHub Desktop.
Mistake on 2/1 of Dep Type Talk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait IsFuture[F]{ | |
| type T | |
| def apply(f: F): Future[T] | |
| } | |
| object IsFuture{ | |
| def apply[F](implicit isf: IsFuture[F]) = isf | |
| implicit def mk[A] = new IsFuture[Future[A]]{ | |
| type T = A | |
| def apply(f: Future[A]): Future[A] = f //it really should read as an identity. | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Ok, take #1 was an awful, confused, unhelpful... I need to put more thought into these things when helping people. | |
| //This is a contrived example. | |
| trait Result[+A]{ | |
| def map[B](f: A => B): Result[B] | |
| def recover[B >: A](implicit isf: IsFuture[B]): PartialFunction[Throwable, isf.A] => Result[B] | |
| } | |
| case class Recoverable[+A](value: A) extends Result[A]{ | |
| def recover[B >: A](implicit isf: IsFuture[B]) = f: PartialFunction[Throwable, isf.A] => map(isf(value) recover f) | |
| } | |
| //Better, is the first half of the talk from NE Scala that I gave this year. | |
| trait IsList[LA]{ | |
| type A | |
| def apply(la: LA): List[A] | |
| } | |
| object IsList{ | |
| def apply[LA](implicit isl: IsList[LA]): Aux[LA, isl.A] = isl | |
| type Aux[LA, A0] = IsList[LA]{ type A = A0 } | |
| implicit mk[A0] = new IsList[List[A0]]{ | |
| type A = A0 | |
| def apply(la: List[A0]) = la | |
| } | |
| } | |
| case class Items[A](value: A){ | |
| //Here, this is acting as an implicit guard. You can't call this function unless `A` is a `List`. | |
| //This is very similar to `sum` from `List` itself where you can only call it if `A` has a `Numeric[A]` type class. | |
| def fullLength(implicit isl: IsList[A]) = isl(value).length | |
| } |
Author
Gah! That second thing is slightly wrong. Pass[B] should be Pass[Future[B]].
I'm still not seeing how it all fits together. There seems to be a couple of missing pieces:
In the case of Fail, you're passing in a Throwable, however isf.apply expects a type A.
In the case of Pass, you use invariant type param A, so Pass[A] is not a subclass of Pass[B]. You can't change this to covariant because of the implicit IsFuture[A] parameter.
As a result, the above code does not compile.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the next slide, you define the
logResultmethod:What could you pass into this method as
Thing? An example/explanation would help a lot :)