]> git.mdlowis.com Git - archive/dlang.git/commitdiff
Added a simple example of creating classes in dlang
authorMike D. Lowis <mike@mdlowis.com>
Sun, 1 Apr 2012 13:34:47 +0000 (09:34 -0400)
committerMike D. Lowis <mike@mdlowis.com>
Sun, 1 Apr 2012 13:34:47 +0000 (09:34 -0400)
example.dl

index 582542be08933bf02ec064e23d86ca2ceeaceb79..6f2f20307881053c853caaea45283b64a5ccea90 100644 (file)
@@ -137,31 +137,28 @@ foo = force foo
 #------------------------------------------------------------------------------
 
 # Define a macro for creating new classes
-#% new [
-#    (I B) : ({
-#        this := @{ $proto : $1 }
-#        $2()
-#        this
-#    })(),
-#
-#    (B) : ({
-#        this := @{}
-#        $2()
-#        this
-#    })()
-#]
-
-#Duck := new foo {
-#    sound := "Quack"
-#    this.mating_call = { sound }
-#}
-#
-#Cow := new foo {
-#    sound := "Moo"
-#    this.mating_call = { sound }
-#}
-
-#print( Duck )
-#print( Duck[$mating_call] )
-#print( (Duck[$mating_call])() )
+% class [
+    (B)   : {
+        this := @{}
+        $1()
+        this
+    },
+]
+
+# Simple class definition (No inheritance supported)
+Animal := class {
+    # The sound of the mating call is kept private
+    priv_call := "Moo!"
+
+    # Define a method to print the mating call sound
+    this.mating_call = {
+        print( priv_call )
+    }
+}
+
+# Animal is a constructor so call it to create a new cow
+Cow := Animal()
+
+# And call the method to print the sound
+(Cow.mating_call)()