Changeset 21941 in project for gazette

12/13/10 12:07:07 (9 years ago)

gazette: clarified development section thanks to peter, removed recipe

1 edited


  • gazette/src/issues/

    r21940 r21941  
    3636* Ticket #444 Symbol lookup fails in modules in conjunction with
    37   renaming macros: [[user:peter-bex|Peter Bex]] came up with a patch to
    38   fix this bug in the IR-macro expander.
     37  renaming macros: [[user:peter-bex|Peter Bex]] came up with a patch
     38  to fix this bug which turned out to be of a more general nature than
     39  just affecting the IR-macro expander.
    4041Also chicken now has another scrutinizer mode called
    4344not perform a self-call.'' as Felix explains on
     47The debug switch ''-:d'' is now also handed to the ''chicken'' program
     48by the compiler driver ''csc''.
    4650== 3. Chicken Talk
    7781== 4. Omelette Recipes
    79 For this issue I want to take you on a tour through chicken scheme's
    80 object system [[egg:coops|coops]]. It is the successor of the older
    81 and now unsupported [[egg:tinyclos|tinyclos]] extension. So if you
    82 have been relying on tinyclos you should consider switching to coops.
    84 Now I can hear some of you say: "Wait, an object system for a
    85 funcitonal language?". Yes! Scheme is much more than a functional
    86 language, it is a ''multi-paradigm'' language meaning that you can
    87 choose the programming paradigm to fit your problem. Functional,
    88 imperative and object oriented, all is possible with scheme.
    90 The main goal of objects is to hide (state) information from its
    91 users. You should not be concerned with the nitty gritty
    92 implementation details of a function (and not messing with it!), thus
    93 objects provide a clean interface to your program. This interface is
    94 used by sending messages in one form or another to your object.  Not
    95 covered by this definiton of object orientation is the notion of a
    96 'class hierarchy'' and ''inheritance''. Languages like C++ and Java
    97 heavily rely on this paradigm. In this discussion classes are general
    98 plans of how objects will look like, while objects are the "real
    99 thing". Some people call objects instances of classes.
    101 In scheme you can already implement objects (without classes) using a
    102 dispatching method and you may have already seen the famous bank
    103 account example by Abelson / Sussman in Structures and Interpretation
    104 of Computer Programs. If not you can look it up in
    105 [[|section
    106 3.1.1]]. Here the implementation details are hidden inside the
    107 dispatch procedure.
    109 Java and C++ also take information hiding to the extreme. Not only the
    110 object's state is hidden behind walls but also the state manipulating
    111 code, which is called ''methods''. What this means in practise is that
    112 you need to assign a method to an object while planning your
    113 abstractions. In most cases this works well. In others it will become
    114 a deliberate design decision where to put a particular method.
    116 A short example: Imagine you have a class ''human'' which is inherited
    117 by a class ''male'' and ''female''. Now as we all have an urge to
    118 reproduce where to put a method for having sex? Create a method
    119 ''haveSex'' in the human class, duplicate it in ''male'' or
    120 ''female''? What would the argument to such a class be? What about
    121 having sex with people of the same sex, toys, animals, buildings...
    123 Depending on your willingness to imagine all these things you will
    124 have to make a deliberate design decision here. One that will affect
    125 on how you see the world.
    127 With scheme being a lisp you are in for a surprise: Its designers have
    128 taken a slightly different approach to object orientation: a dynamic
    129 object system centered around objects, classes and ''generic
    130 functions''. Mostly these systems have been inspired by the ''Common
    131 Lisp Object System (CLOS)''. Coops is no exceptions. Ok, we do know
    132 what objects and classes are but what are generic functions?
    134 First of all classes in coops look like this:
    136 <enscript highlight="scheme">
    137 (define-class <human> () name birthdate children)
    138 (define-class <male> (<human>) testosteron-level)
    139 (define-class <femals> (<human>) estrogen-level)
    140 </enscript>
    142 By tradition class names are encapsuled in ''<'' and ''>'' to
    143 distinguish them form ordinary symbols.
    145 As you can see coops' classes only hold on to their precious state. Methods
    146 are defined ''separately'' from their classes. So for our human
    147 example we declare a generic function called ''have-sex!'' and define
    148 the methods for each class combination we want to imagine separately!
    150 <enscript highlight="scheme">
    151 (define-generic (have-sex! a b))
    153 ; the most common case
    154 (define-method (have-sex! (male a) (female b))
    155                (let ((newborn (make-human "Bobby" (current-seconds) '())))
    156                     (set! (slot-value a children) (cons newborn (slot-value a children))))
    157                     (set! (slot-value a children) (cons newborn (slot-value a children))))
    159 ; all other cases (greatly simplified!)
    161 (define-method (have-sex! (male a) (male b) 'just-fun))
    162 (define-method (have-sex! (female a) (female b) 'just-fun))
    164 ; even more cases are up to your imagination....
    165 </enscript>
    167 So you can see here that we can provide sex recipies independently to
    168 the kind (and number) of participants. We could even change and extend
    169 our repertoire later should the need arise.
    171 Now to a more theoretical example with less tendency to need parental
    172 discretion: a game. Inspired by lisp tutorials we will see how the
    173 coops object model can be used to create a small work of interactive
    174 fiction.
    17784== 5. About the Chicken Gazette
Note: See TracChangeset for help on using the changeset viewer.