Felix Mulder
Felix Mulder
Felix Mulder
trait Super {
def x: String
println(s"initialized with: $x")
}
class Tester extends Super {
val x = "wat"
}
"initializd with: null"
public interface Super {
default public void $init$() {
println("initialized with: " + x());
}
public String x();
}
public class Tester implements Super {
private final String x;
public Tester() {
Super.super.$init$();
x = "wat";
}
public String x() { return this.x; }
}
def merp[A,B](a: A, f: A => B): B = f(a)
merp(1, x => x * 2) // error: missing parameter type
def merp[A,B](a: A)(f: A => B): B = f(a)
merp(1)(x => x * 2) // 2
trait ListOf[+A] {
def foldLeft1[B](z: B, f: (B, A) => B) = ???
foldLeft1(List.empty, (b, a) => b) // 1
foldLeft1(List.empty, (b, a) => a :: b) // 2
def foldLeft2[B](z: B)(f: (B, A) => B) = ???
foldLeft2(List.empty) { (b, a) => b } // 3
foldLeft2(List.empty) { (b, a) => a :: b } // 4
}
scalac compiles what?
trait ListOf[+A] {
def foldLeft1[B](z: B, f: (B, A) => B) = ???
foldLeft1(List.empty, (b, a) => b) // 1
foldLeft1(List.empty, (b, a) => a :: b) // 2
def foldLeft2[B](z: B)(f: (B, A) => B) = ???
foldLeft2(List.empty) { (b, a) => b } // 3
foldLeft2(List.empty) { (b, a) => a :: b } // 4
}
scalac compiles what?
trait ListOf[+A] {
def foldLeft1[B](z: B, f: (B, A) => B) = ???
foldLeft1(List.empty, (b, a) => b) // 1
foldLeft1(List.empty, (b, a) => a :: b) // 2
def foldLeft2[B](z: B)(f: (B, A) => B) = ???
foldLeft2(List.empty) { (b, a) => b } // 3
foldLeft2(List.empty) { (b, a) => a :: b } // 4
}
Dotty compiles what?
trait ListOf[+A] {
def foldLeft1[B](z: B, f: (B, A) => B) = ???
foldLeft1(List.empty, (b, a) => b) // 1
foldLeft1(List.empty, (b, a) => a :: b) // 2
def foldLeft2[B](z: B)(f: (B, A) => B) = ???
foldLeft2(List.empty) { (b, a) => b } // 3
foldLeft2(List.empty) { (b, a) => a :: b } // 4
}
Dotty compiles.
trait ListOf[+A] {
def foldLeft1[B](z: B, f: (B, A) => B) = ???
foldLeft1(List.empty, (b, a) => b) // 1
foldLeft1(List.empty, (b, a) => a :: b) // 2
def foldLeft2[B](z: B)(f: (B, A) => B) = ???
foldLeft2(List.empty) { (b, a) => b } // 3
foldLeft2(List.empty) { (b, a) => a :: b } // 4
}
1 + 2
(+)
/ \
(1) (2)
Apply(Select(Lit(1), $plus), List(Lit(2)))
def foo {}
// error!
def foo = {}
// res: Unit
But there’s a flag for that: -language:Scala2
A & B
A | B
trait A
trait B
trait C extends B with A // ==> A & B is a supertype of C
class A(val i: Int)
class B(val i: Int)
def p1(implicit ev: A <:< (A | B)) = ???
def p2(implicit ev: B <:< (A | B)) = ???
def p3(implicit ev: (A | B) <:< AnyRef) = ???
def p4(implicit ev: (A | B) <:< AnyVal) = ??? // does not compile
final class SuperHipsterInternalAPI {
def foo(a: Any) = ??? // NO!
def foo(a: TypeA | TypeB) = ??? // YES!
}
Precise Types Bring Performance - Dmitry Petrashko
trait Super {
def x: String
println(s"initialized with $x")
}
class Tester extends Super {
val x = "wat"
}
new Tester
// prints: "initialized with null"
trait Super(x: String) {
println(x)
}
class Foo extends Super("wat")
new Foo
// prints: "initialized with wat."
def storeUser(u: User): DBIO[User] = withCtx { implicit transact =>
// much boilerplate
}
def storeUser(u: User): DBIO[User] = {
// impossibru in scalac
}
implicit Context => R
trait ImplicitFunction1[-T,+R] {
def apply(i: T): R
}
// In the api somwehere:
type DBIO[T] = implicit Transaction => T
// In user code:
def storeUser(u: User): DBIO[User] = {
// ctx available!
}
def ctx: Transaction[Context] = implicitly[Context]
Implicit Functions - Martin OderskyGet involved today!
@rewrites object rules {
def toIsEmpty[T](xs: List[T]) =
Rewrite(
from = xs.length == 0,
to = xs.isEmpty
)
def customFancyWarning(x: Int | Double | ... | Numeric[_]) =
Warn(
pattern = x / 0,
msg = "division by zero, you fool!"
)
}
public double average(int[] data) {
int sum = 0;
for(int i = 0; i < data.length; i++) {
sum += data[i];
}
return sum * 1.0d / data.length;
}
def average(xs: Array[Int]) =
xs.reduce(_ + _) * 1.0 / xs.size
Java | Scala |
45 msec | 872 msec |
public double average(int[] data) {
int sum = 0;
for(int i = 0; i < data.length; i++) {
sum += data[i];
}
return sum * 1.0d / data.length;
}
def average(xs: Array[Int]) =
xs.reduce(_ + _) * 1.0 / x.size
def reduce(op: Function2[Obj, Obj, Obj]): Obj = {
var first = true
var acc: Obj = null
this.foreach { e =>
if (first) {
acc = e
first = false
} else acc = op.apply(acc, e)
}
acc
}
def foreach(f: Funtion1[Obj, Obj]) {
var i = 0
val len = length
while (i < len) {
f.apply(this(i))
i += 1
}
}
trait T[A1, A2, ..., An]
how many combinations?
trait T[A1, A2, ..., An] {
def foo[B1, B2, ..., Bm] = ???
}
and now?
@specialized
@specialized
@specialized(Int)
trait Function3[-T1, -T2, -T3, +R]
def foo[T](x: Seq[T], id: Int) = x(id)