Skip to content

Instantly share code, notes, and snippets.

@wheaties
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save wheaties/ba1993e681f96b071655 to your computer and use it in GitHub Desktop.

Select an option

Save wheaties/ba1993e681f96b071655 to your computer and use it in GitHub Desktop.
Mistake on 2/1 of Dep Type Talk
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.
}
}
//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
}
@yakticus
Copy link
Copy Markdown

In the next slide, you define the logResult method:

def logResult[Thing](thing: Thing)
  (implicit isf: IsFuture[Thing]): Future[isf.T] = 
    isf(thing) map{ x =>
      log info s"I got a result of $x"
      x
    }

What could you pass into this method as Thing? An example/explanation would help a lot :)

@wheaties
Copy link
Copy Markdown
Author

Gah! That second thing is slightly wrong. Pass[B] should be Pass[Future[B]].

@yakticus
Copy link
Copy Markdown

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