From: Brian H. <bh...@sp...> - 2004-04-28 03:59:27
|
On Sun, 25 Apr 2004, Henri DF wrote: > thanks for the pointer. i (naively) thought that anything which "takes > away > polymorphism" could only improve efficiency, and that this would extend to > functors. > > [OT] i wonder when there is ever a case for functors then.. > When you want to bind certain behaviors to a type. A good example of this is the Set module. One set can only be a subset of another set if and only if they have the same ordering (I know this isn't good mathematics, but it's good programming). The ordering of the set should then be bound to the type- two different sets of the same type but with different orders should be different types of sets. Modules and functors allow you to enlist the type checker to enforce this constraint. Don't use functors for optimization, use them to check correctness. By the way: anything that has a .mli file qualifies as a module. So to create a set of strings module, all you need to do is: module StringSet = Set.Make(String);; I just checked that this worked in 3.07- works like a charm. On my wish list: int, bool, float, and char modules. These would take a lot of the pain out of using functors. -- "Usenet is like a herd of performing elephants with diarrhea -- massive, difficult to redirect, awe-inspiring, entertaining, and a source of mind-boggling amounts of excrement when you least expect it." - Gene Spafford Brian |