From: Nicolas C. <war...@fr...> - 2003-10-06 19:40:45
|
> > > Hi people ! > > > > > > Right now, SF downloads are showing that there is (only) around 60 peoples > > > that downloaded the ExtLib (no counts on how many are actually using it :-). > > > > Well, I'm concerned still with the semantics of Enum, > > and since Enum-extending other data structures is a key > > feature .. > > I agree. My laundry list of things I don't like about the current enum > implementation: > > 1) Creating new enums is clumsy. > > 2) The next function returns 'a and throws an execption to indicate the > end of elements in one place, and returns 'a option and returns None to > indicate the end of elements in another. Your eternal battle here :) Not yet convinced that exceptions are better - faster - for internal usage, but dangerous for the programmer if raised from the API ? BTW, there is no Enum.next in the API : it's called Enum.get > 3) Next both returns the "current" element and moves the enumeration > forward to the next element. These two different operations should be > seperate functions. There is already such separates functions : Enum.peek return the current element without removing it Enum.junk remove an element from the enum > 4) Imperitive/side effect driven. Given a choice, I prefer functional > semantics. Note that you can still do enumerations of imperitive > functions (like reading a file). What you do is cache the next object, > making a "pseudo-functional" enumeration object. If it's already returned > a next enum, you return the cached value. If it hasn't, it creates a next > element, caches it, and returns it. Effectively, the enumerations become > a singly linked list. > > I came up with a brilliant solution to all the of above, based upon > objects and inheritance (IMHO an enumeration is an object). > Unfortunately, I needed the following class to be typeable in Ocaml: > > class virtual ['a] enum = > object > method virtual map: ('a -> 'b) -> 'b enum > end;; > > Unfortunately, this doesn't work (in 3.06 or 3.07). Not being able to do > this, my next best solution meant every time you called step (to move the > enumeration forward) on an enumeration that had been mapped n times, you > needed to create n+1 new objects, and calling current (to return the > current element) you needed to create n option variables. Which is why I > haven't posted the code to the list :-). Your answer here : class virtual ['a] enum = object method virtual map : 'b . ('a -> 'b) -> 'b enum end BTW, objects are certainly the worst part of OCaml. They are tricky, difficult to use, and have bad performances (compared to modules instances). If you look at Java programmers (or most of the OO programmers actually) they're using OO only for prototyping ( interfaces in Java or pure virtual classes in C++ ). In Ocaml , you can already prototype by using a module and add specialisation with some polymorphic closures. Believe me : you don't need objects. Nicolas Cannasse |