Using Functional Objects in various languages
started 5/10/2002; 1:33:30 PM - last post 5/11/2002; 4:27:38 AM
|
|
pixel - Using Functional Objects in various languages
5/10/2002; 1:33:30 PM (reads: 1911, responses: 4)
|
|
Using Functional Objects in various languages |
Functional Objects are non-mutable objects. When you modify an object
it returns the modified object instead of doing it in place.
A problem occurs in statically-typed languages because you can't
specialize the return value as wanted/needed.
Also talking about multi-methods...
I tried this after some discussion on comp.lang.functional. I couldn't find a good C++ solution. Some help anyone?
(I hesitated between OOP and functional category, maybe we could
choose more than one? :p)
Posted to object-functional by pixel on 5/10/02; 1:36:02 PM
|
|
|
|
Ehud Lamm - Re: Using Functional Objects in various languages
5/10/2002; 1:46:18 PM (reads: 1131, responses: 0)
|
|
Maybe we need an FOOPL category?
|
|
Oleg - Re: Using Functional Objects in various languages
5/10/2002; 2:42:52 PM (reads: 1131, responses: 0)
|
|
You might find then the following useful:
The present code implements a classless, delegation-based OO system,
similar to those of Self or Javascript. This is a full-fledged OO system
with encapsulation, object identity, inheritance and polymorphism. It is
also a purely functional system: there is not a single assignment or
other mutation in the code below.
http://pobox.com/~oleg/ftp/Scheme/index.html#pure-oo
The code archive
http://pobox.com/~oleg/ftp/Computation/Subtyping/Shapes/
contains a third directory, Shapes, with three source code files. The
files illustrate various ways of handling a collection of polymorphic
values. The collection is populated by Rectangles and Ellipses. They
are instances of concrete classes implementing a common abstract base
class Shape. A Shape is an existential type that knows how to draw,
move and resize itself. A file Shapes-oop.cc gives the conventional,
OOP-like implementation, with virtual functions and such. A file
Shapes-no-oop.cc is another implementation, also in C++. The latter
follows BRules, has no assignments or virtual functions. Any
particular Shape value is created by a Shape constructor and is not
altered after that. Shapes-no-oop.cc achieves polymorphic programming
with the full separation of interface and implementation: If an
implementation of a concrete Shape is changed, the code that
constructs and uses Shapes does not even have to be recompiled! The
file defines two concrete instances of the Shape: a Square and a
Rectangle. The absence of mutations and virtual functions guarantees
that any post-condition of a Square or a Rectangle constructor implies
the post-condition of a Shape. Both shapes can be passed to a
function set_dim(const Shape& shape, const float width, const float
height); Depending on the new dimensions, a square can become a
rectangle or a rectangle square. You can compile Shapes-no-oop.cc and
run it to see that fact for yourself.
It is instructive to compare Shapes-no-oop.cc with Shapes-h.hs, which
implements the same problem in a purely functional, strongly-typed language
Haskell. All three code files in the Shapes directory solve the same problem
the same way. Two C++ code files -- Shapes-oop.cc and Shapes-no-oop.cc -- look
rather different. On the other hand, the purely functional Shapes-no-oop.cc and
the Haskell code Shapes-h.hs are uncanny similar -- in some places,
frighteningly similar.
|
|
pixel - Re: Using Functional Objects in various languages
5/10/2002; 5:30:47 PM (reads: 1132, responses: 0)
|
|
-
Why not this simpler
Shapes-no-oop2.cc
instead of your Shapes-no-oop.cc ?
- In my examples, I tried to get as strong a typing as possible. In
your example that would be having Square::offset_by return a Square
instead of returning Shape.
This kind of problem you don't have with classical OO since after
using square.offset_by(...), you still know square is Square. Whereas
with functional-OO, you loose this information. This is bad because
you usually have special functions/methods for Square that you don't
have for other classes.
|
|
pixel - Re: Using Functional Objects in various languages
5/11/2002; 4:27:38 AM (reads: 1089, responses: 0)
|
|
Hum, I was wrong, one can specialize offset_by :: Shape -> Shape with
offset_by :: Rectangle -> Rectangle.
The problem that do exist is when you want to write a polymorphic
function that works on any Shape. C++ function templates solve this
problem (not very easy to write though).
|
|
|
|