Betekintés: Háber János - Scala, A Java jövője, oldal #2

Figyelem! Ez itt a doksi tartalma kivonata.
Kérlek kattints ide, ha a dokumentum olvasóban szeretnéd megnézni!

stem.nanoTime()
val result = func
val elapsed = System.nanoTime() - start
println("The execution of this call took: %s ns".format(elapsed))
result
}
def myCallback = {
Thread.sleep(1000)
"I'm a callback"
}
val result = measure(myCallback)
> The execution of this call took: 1002449000 ns




High-order funkciók
import org.scalatest.FunSuite
import scala.collection.mutable.Stack
class ExampleSuite extends FunSuite {
test("pop is invoked on an empty stack") {
val emptyStack = new Stack[Int]
intercept[NoSuchElementException] {
emptyStack.pop()
}
assert(emptyStack.isEmpty)
}
}




Scala - minta illesztés
- Hasonló a Java switch-hez
- Bármilyen típusú adatra használható
- Első találat szabály
def matchTest(x: Any): Any = x match {
case 1 | 2 | 3 => "one"
case "two" => 2
case a: Int if (a > 100) => a - 3
case b: Int => b + 3
case _ => "None of above"
}




Option
def div(a:Int)(b:Int):Option[Int] = if (b <= 0) None
else if (a < b) Some(0)
else Some(1 + div(a - b)(b).get)
div(25)(5) // => Some(5)
div(150)(2) // => Some(75)
div(13)(4) // => Some(3)
div(13)(0)
div(25)(-5)

// => None
// => None

div(13)(0) match {
case Some(x) => println(x)
case None => println("Problems")
}
// => prints "Problems"
div(25)(5) match {
case Some(x) => println(x)
case None => println("Problems")
}
// => prints "5"




Implicit konverzió
implicit def doubleToInt(d: Double) = d.toInt
val x: Int = 42.0

//Java
public <T> void javaNoManifest(Class<T> clazz) {
//...
}

//Scala
def manifestTest[T](implicit m:Manifest[T]) = {
m.runtimeClass
}




Operátor túlterhelés
class Complex(val real : Double, val imag : Double) {
def +(that: Complex) =
new Complex(this.real + that.real, this.imag + that.imag)
def -(that: Complex) =
new Complex(this.real - that.real, this.imag - that.imag)
override def toString = real + " + " + imag + "i"
}
object Complex {
def main(args : Array[String]) : Unit = {
var a = new Complex(4.0,5.0)
var b = new Complex(2.0,3.0)
println(a) // 4.0 + 5.0i
println(a + b) // 6.0 + 8.0i
println(a - b) // 2.0 + 2.0i
}
}




Trait-ek
//Hasonló a Java interfacehez
trait Language {
val name:String
//De tartalmazhat implementációt is
override def toString = name
}




Trait-ek
trait JVM {
override def toString = super.toString + " runs on JVM"
}
trait Static {
override def toString = super.toString + " is Static"
}
//A trait-ek összefűzhetők (a sorrend számít!)
class Scala extends Language with JVM with Static {
val name="Scala"
}
println(new Scala()) // -> "Scala runs on JVM is Static"




Trait-ek
trait Quacking {
def quack() = {
println("Quack quack quack!")
}
}
class Duck {
val version = "ACME Inc. Generic Duck v1.0"
}
val aDuck = new Duck with Quacking
aDuck.quack()




Case Class
abstract class Term
case class Var(name: String) extends Term
case class Fun(arg: String, body: Term) extends Term
case class App(f: Term, v: Term) extends Term

def printTerm(term: Term) {
term match {
case Var(n) =>
print(n)

val x1 = Var("x")
val x2 = Var("x")
val y1 = Var("y")
println("" + x1 + " == " + x2 + " => " + (x1 == x2))
println("" + x1 + " == " + y1 + " => " + (x1 == y1))

case Fun(x, b) =>
print("^" + x + ".")
printTerm(b)
case App(f, v) =>
Console.print("(")
printTerm(f)
print(" ")
printTerm(v)
print(")")

Var(x) == Var(x) => true
Var(x) == Var(y) => false

}
}




Scala Java Interoperability
class Companion {
def hello() { println("Hello (class)") } // [1]
}
object Companion {
def hallo() { println("Hallo (object)") } // [2]
def hello() { println("Hello (object)") } // [3]
}

public class TestCo

«« Előző oldal Következő oldal »»