Skip to content

Instantly share code, notes, and snippets.

@snoble
Created January 11, 2018 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snoble/0385b32e8699843e0b2034b749f66734 to your computer and use it in GitHub Desktop.
Save snoble/0385b32e8699843e0b2034b749f66734 to your computer and use it in GitHub Desktop.
sealed trait TList[X, Z]
case class TCons[X, Y, Z](head: X => Y, tail: TList[Y,Z]) extends TList[X, Z]
case class TEnd[X,Z](f: X => Z) extends TList[X, Z]
object Evaluators {
def safe1[X,Z](x: X, tList: TList[X, Z]): Z = tList match {
case TCons(head, tail) => safe1(head(x), tail)
case TEnd(f) => f(x)
}
// dotty: Warning
// scala: Fine
def unsafe1[X,Z](x: X, tList: TList[X, Z]): Z = tList match {
case TCons(head, tail) => safe1("WTF", tail)
case TEnd(f) => f(x)
}
// dotty: Warning
// scala: Fine
def safe2[X,Z](x: X, tList: TList[X, Z]): Z = tList match {
case tCons: TCons[X, _, Z] => safe2(tCons.head(x), tCons.tail)
case TEnd(f) => f(x)
}
// dotty: Error
// scala: Fine
def unsafe2[X,Z](x: X, tList: TList[X, Z]): Z = tList match {
case tCons: TCons[X, _, Z] => unsafe2("WTF", tCons.tail)
case TEnd(f) => f(x)
}
// dotty: Error
// scala: Error
def safe3[X,Y,Z](x: X, tList: TList[X, Z]): Z = tList match {
case tCons: TCons[X, Y, Z] => safe3(tCons.head(x), tCons.tail)
case TEnd(f) => f(x)
}
// dotty: Fine
// scala: Warning (Erasure)
def unsafe3[X,Y,Z](x: X, tList: TList[X, Z]): Z = tList match {
case tCons: TCons[X, Y, Z] => unsafe3("WTF", tCons.tail)
case TEnd(f) => f(x)
}
// dotty: Error
// scala: Error
}
@aaronlevin
Copy link

sealed trait TList[X, Z]
case class TCons[X, Y, Z](head: X => Y, tail: TList[Y,Z]) extends TList[X, Z]
case class TEnd[X,Z](f: X => Z) extends TList[X, Z]

object Evaluators {
  def safe1[X,Z](x: X, tList: TList[X, Z]): Z = tList match {
    case TCons(head, tail) => safe1(head(x), tail)
    case TEnd(f) => f(x)
  }
  // dotty 0.5   : Warning
  // scala 2.11.8: Fine
  // scala 2.12.4: Fine

  def unsafe1[X,Z](x: X, tList: TList[X, Z]): Z = tList match {
    case TCons(head, tail) => safe1("WTF", tail)
    case TEnd(f) => f(x)
  }
  // dotty 0.5   : Warning
  // scala 2.11.8: Fine
  // scala 2.12.4: Fine

  def safe2[X,Z](x: X, tList: TList[X, Z]): Z = tList match {
    case tCons: TCons[X, _, Z] => safe2(tCons.head(x), tCons.tail)
    case TEnd(f) => f(x)
  }
  // dotty 0.5   : Error
  // scala 2.11.8: Fine
  // scala 2.12.4: Fine

  def unsafe2[X,Z](x: X, tList: TList[X, Z]): Z = tList match {
    case tCons: TCons[X, _, Z] => unsafe2("WTF", tCons.tail)
    case TEnd(f) => f(x)
  }
  // dotty 0.5   : Error
  // scala 2.11.8: Error
  // scala 2.12.4: Error

  def safe3[X,Y,Z](x: X, tList: TList[X, Z]): Z = tList match {
    case tCons: TCons[X, Y, Z] => safe3(tCons.head(x), tCons.tail)
    case TEnd(f) => f(x)
  }
  // dotty 0.5   : Fine
  // scala 2.11.8: Warning (Erasure)
  // scala 2.12.4: Warning (Erasure)

  def unsafe3[X,Y,Z](x: X, tList: TList[X, Z]): Z = tList match {
    case tCons: TCons[X, Y, Z] => unsafe3("WTF", tCons.tail)
    case TEnd(f) => f(x)
  }
  // dotty 0.5   : Error
  // scala 2.11.8: Error
  // scala 2.12.4: Error
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment