🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #258

Functional and Object Oriented Languages compared

#258
From: Tim Budd
Date: Wed, 14 Jun 1989 23:06
54 lines
3440 bytes
On the surface, functional programming as in ML and object oriented
programming as in Smalltalk or C++ often seem to be emphasizing different
sides of the same coin.  Let me give a specific example.  Suppose we want
to talk about vehicles as a general category, with specific examples being
cars, trucks and bikes.   Furthermore, we need a function to compute
the weights of such things.  In ML (footnote (1)) we define a union type, 
and a function, as in:

datatype VEHICLE = Car of kind * weight | Truck of weight * kind | Bike;
fun 	weight(Car(_, w)) = w
	| weight(Truck(w,_)) = w
	| weight Bike = 15;

In OOPLS (generic object oriented programming languages) we define a
class Vehicle with subclasses Car, Truck and Bike, and a function weight in 
each.  Now in either case given an instance of a vehicle we can compute its 
weight.  ML chooses to group around functions, while OOPLS group around
classes, but it would seem that the same behavior is available in both. (2)

EXCEPT - there is one very important facility that we can use in functional
languages that we seem not to have available in OOPLS.  Suppose we have
an array of vehicles, and a functional sum that takes a plus reduction of
an array using a user supplied function on each element of the array.
We can compute the sum of the weights of the vehicles using something
like  ``sum (vehiclearray, weight)''.  Since message selectors, such as
``weight'' are not first class entities in OOPLS, there does not seem to
be any similar technique that can be used.  (Before someone else mentions
it, yes I know we could use 
``vehiclearray inject: 0 into: [:x :y | x + y weight]'' in smalltalk, 
but this doesn't seem quite the same and my point that message selectors
are not first class is still valid).

So, the topic for debate today is the question whether method selectors,
such as ``weight'', which are implemented in several classes, should
be considered to be first class objects in their own right.  That is,
should we be able to pass ``weight'' as an argument to another function?
Should be various ``weight'' functions, although implemented in different
classes, be considered to be part of a single entity?
If the answer is yes, what is a reasonable way that this can be implemented?
(3)

footnotes
(1) syntax may not be exactly right, I hope ideas are, however.
(2) given this superficial analysis, there would been to be a slight
efficiency advantage of OOPLS over ML.  OOPLS can select the appropriate
function to execute using a single index into a table (the vtable trick),
but ML seems to require a more general pattern matching facility.
Does anybody have a reference to how exactly ML like languages go about
selecting the right function to execute?  That is, what the code
generated actually looks like?  The best I can see looks like nested
if then else statements.
(3) I have my own ideas, involving stub functions generated on the fly,
but would be interested in seeing if anybody else comes up with something
better.
Technical Details
Message-ID: <11213@orstcs.CS.ORST.EDU>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!orstcs!mist!budd

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!orstcs!mist!budd
From: budd@mist.CS.ORST.EDU (Tim Budd)
Newsgroups: comp.lang.misc
Subject: Functional and Object Oriented Languages compared
Keywords: functional languages object oriented ML smalltalk c++
Message-ID: <11213@orstcs.CS.ORST.EDU>
Date: 14 Jun 89 23:06:53 GMT
Sender: usenet@orstcs.CS.ORST.EDU
Lines: 54