Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: Brian Hurt <bhurt@spnz.org>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] C++ STL and template features compared with OCaml parametric polymorphism and OO features
Date: 28 Sep 2004 02:38:01 +1000	[thread overview]
Message-ID: <1096303081.28613.710.camel@pelican.wigram> (raw)
In-Reply-To: <Pine.LNX.4.44.0409271025360.5809-100000@localhost.localdomain>

On Tue, 2004-09-28 at 01:30, Brian Hurt wrote:
> On 28 Sep 2004, skaller wrote:

> > For templates all you need is a class with an operator()() method.
> > 
> > Dynamic dispatch is only needed if you need
> > run time function variables (first class functions).
> 
> All this means is that the calling code, instead of calling foo->doit(), 
> now instead calls (*foo)().  Not that big of a difference in coding 
> volume.

The call is done by foo() which works for C functions, 
pointers to them, and function objects. This is required for 
STL to be generic.

> And you still need dynamic dispatch because you're passing the superclass
> type in.  

Type information is not required, the C++ compiler checks
the calls argument and return types after instantiation.

> Unless you're talking about templating the map/fold functions so
> that you get a different instantiation for each call?

Yes, because it costs nothing, which is much cheaper
than the virtual dispatch:

template<class F, class T> 
T apply(F f, T a) { return f(a); }

struct Sin { 
  float operator()(float a) const { return a; }
};

float (*pf)(float)= sin;

apply(sin,1.0);
apply(Sin(),1.0);
apply(pf,1.0);

No dynamic dispatch is required here. The templates
works with C functions, pointers to C functions,
and function objects.

The first two calls should both resolve to 
just sin(1.0) (the call through the C pointer
requires an extra memory access .. :)

If you use a base and dynamic dispatch you'd incur
an overhead. Note the call is applied to a function
constant argument (the sine function).

Felix only uses dynamic dispatch when the function
is stored in a variable (or when it is too stupid
to optimise the call away by substitution).

BTW: note in the template, F is NOT the type
of the function -- it is the *class*
of the function. The actual function has type

	T -> T

The class of the function is irrelevant.

The class can even be float (*)(float),
it is still irrelavant -- what matters
is the type of f(a). For the function
object, that's a completely distinct
method with its own type -- unrelated
to the type of F: F is used solely
to lookup operator()() -- oh yeah,
that's ad hoc polymorphism.. :)

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2004-09-27 16:38 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-25 21:12 Vasili Galchin
2004-09-25 21:38 ` Nicolas Cannasse
2004-09-25 22:15   ` Vasili Galchin
2004-09-25 22:52     ` Vasili Galchin
2004-09-26  1:34       ` Jon Harrop
2004-09-26  5:31         ` Radu Grigore
2004-09-26  9:47           ` sejourne_kevin
2004-09-26 13:05           ` Jon Harrop
2004-09-26 14:36             ` skaller
2004-09-26 15:08               ` sejourne_kevin
2004-09-26 15:27                 ` skaller
2004-09-26 18:51               ` Jon Harrop
2004-09-26 20:14                 ` Radu Grigore
2004-09-27  1:59                   ` Jon Harrop
2004-09-27  4:48                     ` skaller
2004-09-27  9:40                       ` Jacques GARRIGUE
2004-09-27 10:50                     ` Radu Grigore
2004-09-27 12:14                       ` skaller
2004-09-27 13:11                       ` Jon Harrop
2004-09-27 13:31                         ` Radu Grigore
2004-09-27 16:54                           ` Jon Harrop
2004-09-29 18:59                             ` Radu Grigore
2004-09-27 13:32                         ` Radu Grigore
2004-09-27 14:04                         ` Brian Hurt
2004-09-27 14:58                           ` skaller
2004-09-27 15:30                             ` Brian Hurt
2004-09-27 16:38                               ` skaller [this message]
2004-09-27 17:01                                 ` Brian Hurt
2004-09-28  1:21                                   ` skaller
2004-09-27 16:41                           ` brogoff
2004-09-28  0:26                             ` skaller
2004-09-29 15:32                         ` Florian Hars
2004-09-29 16:49                           ` [Caml-list] Factoring HOFs [was Re: C++ STL...] Jon Harrop
2004-09-30  9:19                             ` Radu Grigore
2004-09-30 10:13                             ` Keith Wansbrough
2004-09-30 10:31                               ` Keith Wansbrough
2004-09-30 13:21                               ` skaller
2004-09-30 23:17                               ` [Caml-list] Factoring HOFs Jacques Garrigue
2004-10-01  8:46                                 ` Keith Wansbrough
2004-10-01 17:35                                 ` brogoff
2004-09-26 20:43                 ` [Caml-list] C++ STL and template features compared with OCaml parametric polymorphism and OO features skaller
2004-09-26 14:19           ` skaller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1096303081.28613.710.camel@pelican.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=bhurt@spnz.org \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox