Skip to content

Instantly share code, notes, and snippets.

@hossshy
Created January 13, 2016 06:59
Show Gist options
  • Save hossshy/f54cb9c6ee19251f21eb to your computer and use it in GitHub Desktop.
Save hossshy/f54cb9c6ee19251f21eb to your computer and use it in GitHub Desktop.
package chapter3
import java.util.Date
/**
* Created by hoshi on 1/13/16.
*/
object CallByName {
def nano() = {
println("Getting nano")
System.nanoTime()
}
def notDelayed(t: Long) = {
println("In not delayed method")
println("Param:" + t)
t
}
def delayed(t: => Long) = {
println("In delayed method")
println("Param:" + t)
t
}
def main(args: Array[String]) {
println("----")
delayed(nano())
println("----")
notDelayed(nano())
println("----")
val d = new Date()
println(d.getTime) // omit parentheses
println(d toString) // omit dot
}
}
----
package chapter3
/**
* Created by hoshi on 1/13/16.
*/
object Car {
def drive { println("Drive car")}
}
object AppTrait extends App {
Car.drive
}
----
package chapter3
import java.util.Date
/**
* Created by hoshi on 1/12/16.
*/
class CodeBlocks {
def meth9() = "Hello World"
def meth3():String = {"Moof"}
def meth4():String = {
val d = new Date()
d.toString
}
val x3:String = {
val d = new Date()
d.toString
}
}
object CodeBlockMain {
def main(args: Array[String]) {
val cb = new CodeBlocks
println(cb.meth4)
println(cb.x3)
}
}
----
package chapter3
import scala.util.{Random => Rdm} // rename
/**
* Created by hoshi on 1/13/16.
*/
trait Shape2 {
def area: Double
}
object Shape2 {
private class Circle(radius: Double) extends Shape2 {
override val area = 3.14 * radius * radius
}
private class Rectangle(height: Double, length: Double) extends Shape2 {
override val area = height * length
}
def apply(height: Double, length: Double): Shape2 = new Rectangle(height, length)
def apply(radius: Double): Shape2 = new Circle(radius)
}
object Companion extends App {
val circle = Shape2(2)
println(circle.area)
val rect = Shape2(2, 3)
println(rect area)
println(Rdm.nextInt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment