On 02-04-2008, salog <[EMAIL PROTECTED]> wrote: > >> He should have used functor a little bit more >> >> module type E = >> sig >> type t >> val of_int: int -> t >> val add: t -> t -> t >> end >> ;; >> >> module Add3 (Elem: E) = >> struct >> type t = E.t >> >> let n3 = >> E.of_int 3 >> >> let add3 z = >> E.add z n3 >> end >> ;; >> >> module Add3Nativeint = Add3(Nativeint) >> ;; >> >> module Add3Int64 = Add3(Int64) >> ;; >> >> module Add3Int32 = Add3(Int32) >> ;; > > I used C# for a while and this example reminds me generics in this > language. > But generics are more laconic: you have to only declare a name of > generic type and then to indicate a type of treating object like this: > List<string>; > or > List<int32>; >
Don't be mislead... Parametrized class (like the List<XXX>) in C++ (and maybe in C#) are "text replacement". These are piece of crap and one of the reason i stop coding using C++. In particular, it requires to compile every time the same code (with some string replaced). Module and functor in OCaml are not perfect but are far better than this (BTW, this kind of thing is a direct legacy of the famous COBOL "COPY ... REPLACING ..." -- C++ is somehow lagging 30 years behind OCaml). > Ideally I would imagine the solution in OCAML somehow wihout this > declaring several similar functions where types of the argument are > "encoded" in their names. > Is it possible to declare functions with same names but with a > different type of the argument, just as it's realized in C++? > Polymorphic functions just like in C++... The answer is no. There is no way to do int without creating a variant type that unify your type (MyInt32 of Int32.t | MyInt64 of Int64.t | MyInt of int). Polymorphic function is just a syntactic sugar. Even if it seems cool, it brings a lot of unnecessary complexity in your code (i.e. never being sure of what function you are calling because of implicit type cast). I learn to live without it... And now i really don't want to go back to this (having worked with legacy code and this kind of things told me that application written with this syntactic sugar are very hard to maintain). Regards, Sylvain Le Gall --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "ocaml-developer" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/ocaml-developer?hl=en For other OCaml forums, see http://caml.inria.fr/resources/forums.en.html -~----------~----~----~----~------~----~------~--~---
