Status
Not open for further replies.

newton_meter

Ars Scholae Palatinae
1,424
Originally posted by w.anders:
Does Scheme do object orientation? What does it look like?

That depends on what you mean by object orientation. I'm not trying to be flippant.

The answer to the question I think you're asking is "no", Scheme does not have direct built-in language support for encapsulation, protection, ad hoc polymorphism, and inheritance. All of those things can be implemented a la carte as needed in Scheme. The proper way (of course, IMO) to think of Scheme is the linguistic glue that lets you stick things like that together. Scheme programmers tend to think of programming as language extension: any sufficiently interesting C++ program is just an extension of the programming language C++ to fit a specific problem domain. Thus, Scheme is a language optimized for its own extension.

I looked at SML, but the tutorial I followed didn't really make me any wiser as how to object orientation looks in a functional language.

Well, if you're a C/C++/Java programmer, you probably see OO as a liberation from a world that doesn't include first class functions :) A functional programmer would see OO as a prison that restricts first class functions to a particular pattern. Objects (to a functional programmer) look like closures with (perhaps) mutable local state and multiple entry points. That's OK, as long as you don't tell me that those are the only closures in the world.

(kg*m*m)/(s*s)
 

w.anders

Ars Tribunus Angusticlavius
6,957
Well, if you're a C/C++/Java programmer, you probably see OO as a liberation from a world that doesn't include first class functions :)
Well, as a software designer I see OO as a damn convenient way of communicating concepts between developers and, to an extent, customers. There are lots of other ways of expression, but OO has shown itself to be extremely useful, I think.
From that perspective, there are benefits with languages that are designed with the intention of making OO straightforward and attractive.

Objects (to a functional programmer) look like closures with (perhaps) mutable local state and multiple entry points.
Would you be interested in showing some code examples of this?

I'm a bit disappointed that there is no "built in" (or perhaps rather predeveloped, a la C++'s Standard Library) OO support in Scheme. Is there a functional language that provides such facilities?
 

newton_meter

Ars Scholae Palatinae
1,424
Originally posted by w.anders:
From that perspective, there are benefits with languages that are designed with the intention of making OO straightforward and attractive.

I certainly didn't intend to disagree with that. Scheme is not designed with such an intention. That's not to say that Scheme doesn't have its own benefits, either.

Would you be interested in showing some code examples of this?

Certainly, in another post.

I'm a bit disappointed that there is no "built in" (or perhaps rather predeveloped, a la C++'s Standard Library) OO support in Scheme. Is there a functional language that provides such facilities?

Well, you will find that there are "non-standard" OO systems built into various Scheme implementations. The O'Caml dialect of ML has support for OO, and the module system of ML gives you a set of features that are normally associated with OO: encapsulation, data hiding, genericity, protection. Haskell's type classes can also be used to easily implement OO features.

Perhaps what's needed (from a functional programming advocacy standpoint) is a tutorial showing how an OO design would be implemented in various functional languages (sort of like the chapters in the Rumbaugh book that show how such a design could be implemented in C, Fortran, (and maybe Ada, I don't have the book handy)).

(kg*m*m)/(s*s)
 

newton_meter

Ars Scholae Palatinae
1,424
A closure is an implementation technique for lexically scoped first-class functions. It is conceptually (though not always implemented as) a pair of a pointer to the code of the function and an environment the function should use to find the values of its free variables. The function is "closed" over the values of its free variables, and thus the term "closure". The following Scheme procedure:

code:<HR>
Code:
(lambda (x) (+ x y))

has a free variable y. Lambda constructs a closure containing the code (that adds x and y) and a way to get to the value of y. A more descriptive name for lambda might be "make-closure".

Switch gears to Java (as a "typical" OO language). When a method refers to a free variable, as in:

code:<HR>
Code:
int f(int x) { return x + y; }

the value of the free variable is found in the object that the method is invoked on. Thus, there is a parallel between objects (which have fields used to find the values of free variables) and closures (which have an environment used to find the values of free variables).

Forget about inner classes (they work too, but don't aid in understanding). Imaigine that all fields are private (they can include getter and setter methods, but the fields themselves are private).

The "difference" between closures and objects is that the code for a closure contains a single entry point; while objects have multiple entry points (one per method). We can simulate multiple entry closures by dispatching on an extra argument (this is effectively the dispatch that an OO language performs on the method name, which may be able to be computed statically; in Scheme, we'll have to write a tiny bit of dispatch code ourselves).

Here's an extremely simple class, in Java:

code:<HR>
Code:
class Point {
  private double x;
  private double y;<P>  public Point(double x, double y) {
    this.x = x;
    this.y = y;
  }<P>  public double getX() { return x; }
  public double getY() { return y; }<P>  public void setX(double newX) { x = newX; }
  public void setY(double newY) { y = newY; }
}

In scheme, we want a "constructor" function that takes an initial x and y value; and builds an "object", a closure that contains x and y as free variables. In addition, we want the entry point of the (returned) closure to dispatch on an extra argument (the method name):

code:<HR>
Code:
(define (make-point x y)
  (lambda (method . args)
    (case method
      ((get-x) x)
      ((get-y) y)
      ((set-x) (set! x (car args)))
      ((set-y) (set! y (car args))))))

Now, we can create points:

code:<HR>
Code:
new Point(a, b) // Java<P>(make-point a b) ; Scheme

And manipulate them:

code:<HR>
Code:
somePoint.getX();
somePoint.setY(Math.sqrt(2));  // Java<P>(some-point 'get-x)
(some-point 'set-y (sqrt 2)) ; Scheme

Writing in the Scheme style doesn't even require much overhead (though my version omits a lot of checking that a Java compiler would perform). A suitable level of macro abstraction can give you all the OO features you'd ever want, in Scheme. However, if you seek to build OO systems, then you should probably stick to one of the non-standard Scheme-based object systems (or look elsewhere). If your goal is to understand how OO systems behave, and experiment with different OO systems, then I can't think of a better vehicle than Scheme.

(kg*m*m)/(s*s)
 

w.anders

Ars Tribunus Angusticlavius
6,957
Perhaps what's needed (from a functional programming advocacy standpoint) is a tutorial showing how an OO design would be implemented in various functional languages (sort of like the chapters in the Rumbaugh book that show how such a design could be implemented in C, Fortran, (and maybe Ada, I don't have the book handy)).
That would certainly be very interesting to see, especially with a few other languages thrown in for contrast.
 
Status
Not open for further replies.