# Has someone implemented the OO-syntax for Harvey's cs61a?

**URL:** <https://racket.discourse.group/t/has-someone-implemented-the-oo-syntax-for-harveys-cs61a/1879>\
**Category:** General\
**Created:** [April 20, 2023, 11:05am UTC](https://racket.discourse.group/t/has-someone-implemented-the-oo-syntax-for-harveys-cs61a/1879 "2023-04-20T11:05:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Dan](https://racket.discourse.group/u/Dan)\
**Post date:** [April 20, 2023, 11:05am UTC](https://racket.discourse.group/t/has-someone-implemented-the-oo-syntax-for-harveys-cs61a/1879/1 "2023-04-20T11:05:49Z")

</div>

Hi,

I'm following Harvey's course and I need to run the OO examples using the library they developed ([here](https://people.eecs.berkeley.edu/~bh/61a-pages/Lib/obj.scm)).

I am not able to load it since `#lang simply-scheme` does not define `define-macro`.

Does anyone by chance implement this OO model?

PS: I am not able to use UCB's Scheme since it does not run on M1 chips.

Best Regards,

Daniel

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [April 20, 2023, 11:12am UTC](https://racket.discourse.group/t/has-someone-implemented-the-oo-syntax-for-harveys-cs61a/1879/2 "2023-04-20T11:12:00Z")

</div>

Try

[sicp/obj.rkt at main · zackads/sicp · GitHub](https://github.com/zackads/sicp/blob/main/inst.eecs.berkeley.edu/~cs61as/fa11/lib/obj.rkt)

---

<div class="post-metadata">

**Author:** ![Dan](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Dan](https://racket.discourse.group/u/Dan)\
**Post date:** [April 21, 2023, 7:02am UTC](https://racket.discourse.group/t/has-someone-implemented-the-oo-syntax-for-harveys-cs61a/1879/3 "2023-04-21T07:02:24Z")

</div>

Hi @soegaard, thanks for the suggestion.

I've tried to use it for two examples and got two different errors.

In the first example, I got an error that I can circumvent by declaring a dummy instance-var. This is the error:

```
obj.rkt:156:5: result arity mismatch;
expected number of values not received
expected: 2
received: 1
in: local-binding form
arguments...:
  '()

```

This is the original code

```
#lang racket
(require "obj.rkt" rackunit)

(define-class (account balance)
   ;(instance-vars (dummy-var 0)) ; -> Commented to generate the error
  (method (deposit amount)
      (set! balance (+ amount balance))
      balance)
  (method (withdraw amount)
      (if (< balance amount)
          "Insufficient funds"
          (begin
            (set! balance (- balance amount))
            balance))))

```

The second error I got when building a class inheriting from this. Here's the error:

```
my-account: unbound identifier in: my-account

```

And here's the code that generated the error:

```
(define-class (checking-account init-balance)
   (parent (account init-balance))
   (instance-vars (check-fee 0.10))
  (method (write-check amount)
      (ask self 'withdraw (+ amount check-fee)))
  (method (set-fee! fee)
      (set! check-fee fee)))

```

This second error I can't circumvent.

Thank you

---

<div class="post-metadata">

**Author:** ![Dan](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Dan](https://racket.discourse.group/u/Dan)\
**Post date:** [April 22, 2023, 7:41am UTC](https://racket.discourse.group/t/has-someone-implemented-the-oo-syntax-for-harveys-cs61a/1879/4 "2023-04-22T07:41:22Z")

</div>

Default-method is generating an error as well:

```
(define-class (squarer)
    (instance-vars (dummy 0)) 
    (default-method (* message message))  
    (method (7) 'buzz) )

```

The error it generates:

```
message: unbound identifier
context...:
other binding...:
other binding...:
common scopes...: in: message

```

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [April 22, 2023, 10:13am UTC](https://racket.discourse.group/t/has-someone-implemented-the-oo-syntax-for-harveys-cs61a/1879/5 "2023-04-22T10:13:41Z")

</div>

I think, you will get a better experience using the builing objects.

```scheme
#lang racket

(define account
  (class object% ; parent
    (super-new)

    (init-field balance)

    (define/public (deposit amount)
      (set! balance (+ amount balance))
      balance)

    (define/public (withdraw amount)
      (if (< balance amount)
          "Insufficient funds"
          (begin
            (set! balance (- balance amount))
            balance)))))

(define my-account (new account [balance 20]))
(send my-account deposit 100)
(send my-account withdraw 20)

(define checking-account
  (class account ; parent
    (init init-balance)
    (super-new [balance init-balance])
    (define check-fee 0.10)
    
    (define/public (write-check amount)
      (send this withdraw (+ amount check-fee)))
    
    (define/public (set-fee! fee)
      (set! check-fee fee))))

(define my-account2 (new checking-account [init-balance 20]))
(send my-account2 write-check 10)

```
