scala(13)A Tour of Scala: Explicitly Typed Self References
Explicitly Typed Self References
…snip…
Subclassing
class Point(xc: Int, yc: Int){
val x: Int = xc
val y: Int = yc
def move(dx: Int, dy: Int): Point =
new Point(x+dx, y+ dy)
}
class ColorPoint(u: Int, v: Int, c: String) extends Point(u, v) {
val color: String = c
def compareWith(pt: ColorPoint) : Boolean =
(px.x == x) && (pt.y == y) && (pt.color == color)
override def move(dx: Int, dy: Int) : ColorPoint =
new ColorPoint(x + dy, y + dy, color)
}
Local Type Inference
Deduce the type from the initialization expression of the variable and return response of the return type.
def main(args: Array[String]): Unit = {
valx = 1 + 2 * 3// the type is Int
def succ(x: Int) :Int = x+ 1// the return type should be :Int and we can ommit it
}
And remember the recursive methods can not deduce the type of return.
Unified Types
All the values are objects in scala.
Variance
package com.sillycat.easyscala.start.tour.tour4
object VariancesTest extends App {
vars: Stack[Any] = new Stack().push("hello");
s = s.push(new Object())
s = s.push(7)
Console.println(s) //7 java.lang.Object@5b6df84b hello
}
class Stack[+A] {
def push[B >: A](elem: B): Stack[B] = new Stack[B] {
overridedef top: B = elem
overridedef pop: Stack[B] = Stack.this
overridedef toString() = elem.toString() + " " +
Stack.this.toString()
}
def top: A = sys.error("no element on stack")
def pop: Stack[A] = sys.error("no element on stack")
overridedef toString() = ""
}
Views
…snip…
XML Processing
…snip...
References:
http://www.scala-lang.org/node/104
http://www.scala-lang.org/node/124
http://www.scala-lang.org/node/125
http://www.scala-lang.org/node/127
http://www.scala-lang.org/node/128
http://www.scala-lang.org/node/130
http://www.scala-lang.org/node/131
scala(12) http://sillycat.iteye.com/blog/1842158
Explicitly Typed Self References
…snip…
Subclassing
class Point(xc: Int, yc: Int){
val x: Int = xc
val y: Int = yc
def move(dx: Int, dy: Int): Point =
new Point(x+dx, y+ dy)
}
class ColorPoint(u: Int, v: Int, c: String) extends Point(u, v) {
val color: String = c
def compareWith(pt: ColorPoint) : Boolean =
(px.x == x) && (pt.y == y) && (pt.color == color)
override def move(dx: Int, dy: Int) : ColorPoint =
new ColorPoint(x + dy, y + dy, color)
}
Local Type Inference
Deduce the type from the initialization expression of the variable and return response of the return type.
def main(args: Array[String]): Unit = {
valx = 1 + 2 * 3// the type is Int
def succ(x: Int) :Int = x+ 1// the return type should be :Int and we can ommit it
}
And remember the recursive methods can not deduce the type of return.
Unified Types
All the values are objects in scala.
Variance
package com.sillycat.easyscala.start.tour.tour4
object VariancesTest extends App {
vars: Stack[Any] = new Stack().push("hello");
s = s.push(new Object())
s = s.push(7)
Console.println(s) //7 java.lang.Object@5b6df84b hello
}
class Stack[+A] {
def push[B >: A](elem: B): Stack[B] = new Stack[B] {
overridedef top: B = elem
overridedef pop: Stack[B] = Stack.this
overridedef toString() = elem.toString() + " " +
Stack.this.toString()
}
def top: A = sys.error("no element on stack")
def pop: Stack[A] = sys.error("no element on stack")
overridedef toString() = ""
}
Views
…snip…
XML Processing
…snip...
References:
http://www.scala-lang.org/node/104
http://www.scala-lang.org/node/124
http://www.scala-lang.org/node/125
http://www.scala-lang.org/node/127
http://www.scala-lang.org/node/128
http://www.scala-lang.org/node/130
http://www.scala-lang.org/node/131
scala(12) http://sillycat.iteye.com/blog/1842158