Methods and properties in scheme - is object oriented programming possible in scheme?

Posted by incrediman on Stack Overflow See other posts from Stack Overflow or by incrediman
Published on 2010-06-02T02:32:31Z Indexed on 2010/06/02 2:43 UTC
Read the original article Hit count: 317

I will use a simple example to illustrate my question. In Java, C, or any other OOP language, I could create a pie class in a way similar to this:

class Apple{
    public String flavor;
    public int pieces;
    private int tastiness;
    public goodness(){
        return tastiness*pieces;
    }
}

What's the best way to do that with Scheme? I suppose I could do with something like this:

(define make-pie
  (lambda (flavor pieces tastiness)
    (list flavor pieces tastiness)))

(define pie-goodness
  (lambda (pie)
    (* (list-ref pie 1) (list-ref pie 2))))

(pie-goodness (make-pie 'cherry 2 5))

;output: 10

...where cherry is the flavor, 2 is the pieces, and 5 is the tastiness. However then there's no type-safety or visibility, and everything's just shoved in an unlabeled list. How can I improve that?

Sidenote: The make-pie procedure expects 3 arguments. If I want to make some of them optional (like I'd be able to in curly-brace languages like Java or C), is it good practice to just take the arguments in as a list (that is treat the arguments as a list - not require one argument which is a list) and deal with them that way?

© Stack Overflow or respective owner

Related posts about Scheme

Related posts about object-oriented