Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Vocabulary test



>>>>> "David" == David Lloyd-Jones <icomm5@example.com> writes:

    David> I'll bite: what's a polymorphic function in English? I
    David> thought the whole function of functions was to return a
    David> single value.

This is not true in Common Lisp or Python, both of which can and do
return multiple values.[1]  But that's not what a polymorphic function
does, anyway.

A polymorphic function accepts arguments of various types and "Does
What I Meant" with them.  Eg, printf.

But a _real_ polymorphic function should be able to accept say two
numbers and add them, regardless of whether they are chars, ints,
floats, or bignums, without need for a format string.  C++ uses static
type analysis of expressions and virtual functions for this; truly
object-oriented languages like LISP are able to distinguish the type
of any object simply by "looking at it."

Polymorphism is not quite the same thing as overloading, which allows
the compiler to select the right function based on variable types, but
cannot handle variables that may contain different types (eg, a C
union).  But the results are quite similar in many cases, and
overloading is more efficient.


Footnotes: 
[1]  This may be going away in Python 2.0, if I recall the proposals
correctly; Python 2.0 will explicitly require you to assign to a tuple
rather than use multiple values.

A typical use in Common Lisp is for functions like integer division.
Normally you just get a single value, the quotient.  But you can ask
for multiple values using a special form, in which case division
returns both the quotient and the remainder.  This is not the same as
returning a vector; if you assign the result of division to a
variable, normally the variable contains a number, not a vector.

-- 
University of Tsukuba                Tennodai 1-1-1 Tsukuba 305-8573 JAPAN
Institute of Policy and Planning Sciences       Tel/fax: +81 (298) 53-5091
_________________  _________________  _________________  _________________
What are those straight lines for?  "XEmacs rules."


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links