![]() |
|
|
|
|
|
|
2
24th July 10:30
External User
Posts: 1
|
Hi,
It seems that you confuse different concepts. A java interface is not an ocaml interface and, morever, ocaml has object-oriented features. In ocaml, you distinguish 3 parts in the language : the core language, the module language and the OO language. Interface is part of the module language. You can read the introduction in the manual here : http://caml.inria.fr/ocaml/htmlman/index.html In OO community, many people confuse "inheritance" and "subtyping". Perhaps more and more people realize that "inheritance" is not "subtyping" ;-). "class" and "inherit" keywords are part of the OO language and there are what you have in java. But, in ocaml, you don't use them a lot because it is not the spirit of the language. Instead, you usualy use the module language. You're right :-). You're also right ;-). It is not the major difference. Java interfaces/classes provide "inheritance" (not "subtyping") and "late binding". Ocaml interfaces do not. In ocaml, you write: module type STACK = sig type t val push : int -> t -> unit val pop : t -> int val is_empty : t -> bool end module Stack : STACK = struct ... end This module is part of of the ocaml standard library : http://caml.inria.fr/ocaml/htmlman/libref/Stack.html It works but it is not beautiful at all, sorry :-). I suggest you read the implementation of stacks in the source code of Objective Caml. The code works beautifully ;-). "class type" and the # syntax are parts of the OO language. In java and ocaml, a class defines a (class) type and a class. But in ocaml, it is possible to only define a class type. Hope this helps, Julien -- mailto:Julien.Signoles@lri.fr ; http://www.lri.fr/~signoles "In theory, practice and theory are the same, but in practice they are different" (Larry McVoy) |
|
|
|
|
5
10th August 10:19
External User
Posts: 1
|
Indeed. But where do you *really* need to return a module, instead of
just a value of some type defined in a module? Well, you have 'include'. Oh, there is no difference in Java between class and interface, and 'inherits' and 'implements' - except that interfaces are subject to certain restrictions compared to (abstract) classes, in order to avoid problems with multiple inheritence. OCaml resolves the MI issues differently, so it does not need this artificial distinction in its object system. Depends on what you mean by "late binding" (it is often used with varying meanings by different people). Modules surely don't give you built-in "open recursion", where you can rewire pre-existing calls, like with inheritence in OO languages. Also, as you noticed, you cannot pass a local module out of scope, so it is rather restricted. OCaml's object types (which are independent of classes) are "first-class", and vastly superior to Java's interfaces in most aspects. They *are* basically "records with a local namespace". You could rephrase your stack type as (caveat: untested) type 'a stack = <push: 'a -> unit; pop: unit -> 'a; isEmpty : unit -> bool> let list_stack () = object val mutable l = [] method push x = l <- x::l method pop = let v = List.hd l in (l <- List.tl l; v) method isEmpty = match l with [] -> true | _ -> false end let s1 = list_stack() in s1#push 10; s1#push 20; s1#pop let s2 = list_stack() in s2#push "hello"; s2#push "world"; s2#pop Are you sure, 'mystack', really? ;-) Note that you can use 'open' with modules, leaving you with put mystack 1 vs. mystack.put 1 Personally, I find the former much more regular. Elsewhere: This is a consequence of OCaml's structural object types. Since you never know the actual class that constructed an object, you cannot assume anything about it. Except for self. Cheers, - Andreas -- Andreas Rossberg, rossberg@ps.uni-sb.de Let's get rid of those possible thingies! -- TB |
|
|
8
10th August 10:19
External User
Posts: 1
|
fishifish@gmail.com (Ben) writes:
Interesting marketing talk :-) From an implementation point of view, multiple inheritance is difficult. It can also be confusing for the programmer. So Java designers decided to do without. But they realized that you sometimes need two classes to share a feature while not coming from the same root, and they had to add interfaces to do that, which are a form of multiple inheritance where only one side is allowed to provide an implementation. The fact you can use interfaces to enforce abstraction doesn't mean that interfaces themselves are abstract. Actually, declaring that a class implements an interface adds some extra code to this class, independently of the required methods. Compare this to ocaml class types. They are just abstract requirements about a class. They generate no code at all, and you can have an object belong to a class type, as soon as it satisfies the requirements, even if its class was defined before the class type. Well, ocaml already had records, and from a practical point of view it seemed better to keep them unchanged. The namespace has an advantage: in ML types are implicit, and this way you can directly deduce the type of a record from any of its fields. (The SML approach has some advantages of its own, though) Ocaml objects can be both simple and complex. But you _can_ just use them as records with a local namespace, and if this is enough for you then you need know nothing about the rest of the object system. Yet you might want to understand a few more features, like polymorphism through #-types and subtyping, which are really just properties of record types. --------------------------------------------------------------------------- Jacques Garrigue Kyoto University garrigue at kurims.kyoto-u.ac.jp <A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A> |
|
|
10
10th August 10:20
External User
Posts: 1
|
[Anybody knows why some of my posts to c.l.ml - e.g. the one Ben is
answering to - do not appear on several News servers (including Google)?] [Moderator: Netnews at CMU has been under repair - that's my best guess as to cause. I've re-submitted your post - apologies to those getting duplicates. Anyone else having problems please let me know at comp-lang-ml-request@cs.cmu.edu. -leaf ] You achieve no more separation using interfaces than you would using abstract classes and plain inheritence. As long as you never need multiple implements/inherits relations in a single class - i.e. MI - interfaces are not required. When you take a deeper look at the Java spec you'll find that there is *absolutely nothing* that interfaces technically give you but classes don't, except for MI! The whole concept of interfaces in Java is basically a deception. OCaml's structural object types OTOH are truly separate from classes: different classes can produce the same object type without having to be in any kind of declared inheritence relation. You can define abstractions over object types without even mentioning classes. That's why I claimed that: With this I totally agree: I also would prefer a more light-weight notion of polymorphic records instead of a full-blown object system. Still, you can use objects that way and ignore classes altogether. It should not be more expensive either, only the syntax for creating object records is slightly more heavy than necessary. Cheers, - Andreas -- Andreas Rossberg, rossberg@ps.uni-sb.de Let's get rid of those possible thingies! -- TB |
|