From: Mike D. Lowis Date: Sun, 1 Apr 2012 13:34:47 +0000 (-0400) Subject: Added a simple example of creating classes in dlang X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=db7b01680d6355fc1a06734a86327d669c8bfc5e;p=archive%2Fdlang.git Added a simple example of creating classes in dlang --- diff --git a/example.dl b/example.dl index 582542b..6f2f203 100644 --- a/example.dl +++ b/example.dl @@ -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)()