Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,24 @@
(lib 'types)
(lib 'struct)
(struct T (integer:x)) ;; super class
(struct S T (integer:y)) ;; sub class
(struct K (T:box)) ;; container class, box must be of type T, or derived
(define k-source (K (S 33 42)))
(define k-copy (copy k-source))
k-source
→ #<K> (#<S> (33 42)) ;; new container, with a S in box
k-copy
→ #<K> (#<S> (33 42)) ;; copied S type
(set-S-y! (K-box k-source) 666) ;; modify k-source.box.y
k-source
→ #<K> (#<S> (33 666)) ;; modified
k-copy
→ #<K> (#<S> (33 42)) ;; unmodified
(K "string-inside") ;; trying to put a string in the container box
😡 error: T : type-check failure : string-inside → 'K:box'

View file

@ -0,0 +1,37 @@
type
T = ref object of TObject
myValue: string
S1 = ref object of T
S2 = ref object of T
method speak(x: T) = echo "T Hello ", x.myValue
method speak(x: S1) = echo "S1 Meow ", x.myValue
method speak(x: S2) = echo "S2 Woof ", x.myValue
echo "creating initial objects of types S1, S2, and T"
var a = S1(myValue: "Green")
a.speak
var b = S2(myValue: "Blue")
b.speak
var u = T(myValue: "Blue")
u.speak
echo "Making copy of a as u, colors and types should match"
u.deepCopy(a)
u.speak
a.speak
echo "Assigning new color to u, A's color should be unchanged."
u.myValue = "Orange"
u.speak
a.speak
echo "Assigning u to reference same object as b, colors and types should match"
u = b
u.speak
b.speak
echo "Assigning new color to u. Since u,b references same object b's color changes as well"
u.myValue = "Yellow"
u.speak
b.speak

View file

@ -0,0 +1,22 @@
class T(value) {
method display {
say value;
}
}
class S(value) < T {
method display {
say value;
}
}
var obj1 = T("T");
var obj2 = S("S");
var obj3 = obj2.dclone; # make a deep clone of obj2
obj1.value = "foo"; # change the value of obj1
obj2.value = "bar"; # change the value of obj2
obj1.display; # prints "foo"
obj2.display; # prints "bar"
obj3.display; # prints "S"

View file

@ -0,0 +1,24 @@
class T {
required init() { } // constructor used in polymorphic initialization must be "required"
func identify() {
println("I am a genuine T")
}
func copy() -> T {
let newObj : T = self.dynamicType() // call an appropriate constructor here
// then copy data into newObj as appropriate here
// make sure to use "self.dynamicType(...)" and
// not "T(...)" to make it polymorphic
return newObj
}
}
class S : T {
override func identify() {
println("I am an S")
}
}
let original : T = S()
let another : T = original.copy()
println(original === another) // prints "false" (i.e. they are different objects)
another.identify() // prints "I am an S"