June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,24 @@
import extensions.
class T
{
Name = "T".
clone = T new.
}
class S :: T
{
Name = "S".
clone = S new.
}
program =
[
type<T> original := S new.
type<T> clone := original clone.
console printLine(original Name).
console printLine(clone Name).
].

View file

@ -0,0 +1,21 @@
object PolymorphicCopy {
def main(args: Array[String]) {
val a: Animal = Dog("Rover", 3, "Terrier")
val b: Animal = a.copy() // calls Dog.copy() because runtime type of 'a' is Dog
println(s"Dog 'a' = $a") // implicitly calls Dog.toString()
println(s"Dog 'b' = $b") // ditto
println(s"Dog 'a' is ${if (a == b) "" else "not"} the same object as Dog 'b'")
}
case class Animal(name: String, age: Int) {
override def toString = s"Name: $name, Age: $age"
}
case class Dog(override val name: String, override val age: Int, breed: String) extends Animal(name, age) {
override def toString = super.toString() + s", Breed: $breed"
}
}