Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Differences

propensive edited this page Oct 6, 2014 · 14 revisions
val x: Byte = 127z
val y: Short = 32767s
val x' = 42
val £ = "Sterling"
val ¥ = "Yen"

The flag -Zirrefutable-generator-patterns disables for-comprehension desugaring to use withFilter. For example, this code:

object Irrefutable {
  for {
    (x, _) <- Some((1, 2))
  } yield x
}

Without -Zirrefutable-generator-patterns will desugar into:

Some(scala.Tuple2(1, 2)).withFilter(((check$ifrefutable$1) => check$ifrefutable$1: @scala.unchecked match {
  case scala.Tuple2((x @ _), _) => true
  case _ => false
})).map(((x$1) => x$1: @scala.unchecked match {
  case scala.Tuple2((x @ _), _) => x
}))

But under -Zirrefutable-generator-patterns, the code will desugar into:

Some(scala.Tuple2(1, 2)).map(((x$1) => x$1 match {
  case scala.Tuple2((x @ _), _) => x
}))

This has a few practical differences:

  • Without the flag, non-matching values will be filtered out
  • With the flag, partial patterns will trigger a warning and scala.MatchError could be thrown at runtime
  • Without the flag, you can't have patterns on the left hand side unless the right hand side has withFilter
  • With the flag, for-comprehension desugaring only needs flatMap and map

It's now possible to customize the error message given upon an ambiguous implicit error. This is feature request SI-6806:

object Ambiguous {
  @typelevel.annotation.implicitAmbiguous("any2stringadd is disabled for ${A}")
  implicit def any2stringaddamb1[A](self: A): any2stringadd[A] = ???

  List(1, 2, 3) + "two"
}

Gives:

/tmp/Ambiguous.scala:5: error: any2stringadd is disabled for List[Int]
  List(1, 2, 3) + "two"
      ^
/tmp/Ambiguous.scala:5: error: value + is not a member of List[Int]
  List(1, 2, 3) + "two"
                ^

Every implicit def, val or var now requires an explicit type ascription with the Z-flag -Zexplicit-implicit-type-ascriptions.

/tmp/explicit.scala:2: error: Implicit definitions must have an explicit type ascription
  implicit val noTypeAscription = 0
                                ^

screen shot 2014-09-27 at 1 06 59 pm

New syntax for type lambdas, removing the need to encode them as type projections:

Simple example:

trait Monad[M[_]]

// the old method using type projections
trait LeftMonad[R] extends Monad[({ type U[x] = Either[x, R] })#U]

// the new method, using an explicit type lambda
trait RightMonad[L] extends Monad[[y] => Either[L, y]]

Here are some other examples:

[x] => (x, x)
[x, y] => (x, Int) => y
[x[_]] => x[Double]
[+x, -y] => Function1[y, x]
Clone this wiki locally