> > I haven't been tricked into reading MJD's article yet, but might your
> > third option be multiple functions with parameter-type-based dispatch?
> > We can do that with perl 5, but it isn't automatic.
> 
> The problem with polymorphic functions is you have to rewrite the
> function N times (where N == the number of different types you want to
> handle).  Its certainly a possiblity, it just seems rather inelegant.
> 
Rather.

Why write the same function multiple times with nothing but the type
specifier changed, when you could just write it once with a type
placeholder? This is all that a template is.

Templates have a bad rep, and fair enough too because their
implementation has been pretty iffy. But think about the reasons there
have been problems:
 - Inconsistent implementation in compilers
   (still no compiler supports all ANSI template features)
 - Frivolous 'variable name too long' warnings because of implementation
 SNAFUs
 - Frivolous 'template depth' errors and warnings
 - Over-enthusiastic use of generative programming (e.g. expression
 templates);
   leads to slow compilation and unearths subtle compiler bugs.

Nothing here is fundamental to the concept of templates. If we stayed
away from trying to use them for compile-time computation (ie generative
programming) and just made them a convenience for identifying type
errors, I think it could work.

The next paragraph is a wild and wacky idea--feel free to ignore it as
being highly eccentric...
----
The idea of a type hierarchy is not mutually exclusive with templates,
BTW. In fact, it's not hard to envisage a template syntax that allows
type placeholders to indicate allowed types (or probably actually
interfaces). The problem in C++ (that doesn't do this) is that the
compiler won't stop you from instantiating a templated class with a type
that is not supported by the class (by 'not supported' I mean that the
type doesn't have an overloaded operator required by the class, resulting
in an error). Imagine something like (imixed up in a strange Perl/C++
blender):

  my list<int> $iList(1,2,3);
  my list<int> $rList = reverse($iList.begin(), $iList.end());
  *** compile error: function 'reverse' requires type supporting
      'bidirectional iterator' interface

-- 
  Jeremy Howard
  [EMAIL PROTECTED]

Reply via email to