(define-module (taste-of-goblins) #:use-module (goblins) #:use-module (goblins actor-lib methods) #:export (^cell ^greeter ^cgreeter ^borked-cgreeter ^car-factory ^borked-car-factory)) ;; define with next argument wrapped in parentheses ;; defines a named function (define (^greeter _bcom our-name) ; constructor (outer procedure) (lambda (your-name) ; behavior (inner procedure) (format #f "Hello ~a, my name is ~a!" ; returned implicitly your-name our-name))) (define (^cell bcom val) (methods ; syntax for first-argument-symbol-based dispatch ((get) ; takes no arguments val ); returns current value ((set new-val) ; takes one argument, new-val (bcom (^cell bcom new-val ))))); become a cell with the new value (define (^cgreeter _bcom our-name) (define times-called ; keeps track of how many times 'greet called (spawn ^cell 0 )); starts count at 0 (methods ((get-times-called) ($ times-called 'get)) ((greet your-name) (define current-times-called ($ times-called 'get)) ;; increase the number of times called ($ times-called 'set (+ 1 current-times-called)) (format #f "[~a] Hello ~a, my name is ~a!" ($ times-called 'get) your-name our-name)))) (define (^borked-cgreeter _bcom our-name) (define times-called (spawn ^cell 0)) (methods ((get-times-called) ($ times-called 'get)) ((greet your-name) (pk 'before-incr ($ times-called 'get)) ;; increase the number of times called ($ times-called 'set (+ 1 ($ times-called 'get))) (pk 'after-incr ($ times-called 'get)) (error "Yikes") (format #f "[~a] Hello ~a, my name is ~a!" ($ times-called 'get) your-name our-name)))) ;; Create a "car factory", which makes cars branded with ;; company-name. (define (^car-factory bcom company-name) ;; The constructor for cars we will create. (define (^car bcom model color) (methods ; methods for the ^car ((drive) ; drive the car (format #f "*Vroom vroom!* You drive your ~a ~a ~a!" color company-name model)))) ;; methods for the ^car-factory instance (methods ; methods for the ^car-factory ((make-car model color) ; create a car (spawn ^car model color)))) (define (^borked-car-factory bcom company-name) (define (^car bcom model color) (methods ; methods for the ^car ((drive) ; drive the car (format #f "*Vroom vroom!* You drive your ~a ~a ~a!" color company-name model)))) ;; methods for the ^car-factory instance (methods ; methods for the ^car-factory ((make-car model color) ; create a car (error "Your car exploded on the factory floor! Ooops!") (spawn ^car model color))))