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

Overloading

This page is part of the web mail archives of SRFI 54 from before July 7th, 2015. The new archives for SRFI 54 are here. Eventually, the entire history will be moved there, including any new messages.



From: soo <tilde@xxxxxxxxxxx>
Subject: Re: a preface
Date: 25 Mar 2004 21:33:24 +0900

>  * From: David Van Horn <dvanhorn@xxxxxxxxxx>
> >> | Why does fmt have two very distinct behaviors?
> 
> Because the required optional arguments are different according to the type.
>
> >> | Why not have two distinct
>    >> | procedures?
> 
> Why must have two procedure?  Inspite of the same processing course and return
> type(string).

I can think of one reason for not overloading the function.
Suppose I like to write a procedure that takes a list of objects,
and print out each element per line, left-padded to 20 columns,
using 'write' representation.

(defun (foo list-of-objects)
  (for-each (cut fmt <> 20 write "\n") list-of-objects))

Oops, this fails if list-of-objects contains numeric values, right?
I think I suppose to write something like this:

(defun (foo list-of-objects)
  (for-each (lambda (elt)
              (if (number? elt)
                 (fmt elt 20 "\n")
                 (fmt elt 20 write "\n")))
            list-of-objects))

However, I think this is awkward.  The trouble is, only the
programmer knows how she wants to treat a given object as 
a numeric value or as just "one of Scheme values"---the program
can't deduct the programmer's intention purely from the type
of the object.

Having two distinct procedures at least help a programmer
to express the intention.

--shiro