Reputation:
I'm trying to write class types and having an issue expressing what I want.
I have
class type event_emitter = object
method on : string -> unit
end
and then I want to do this:
class type socket = object
inherit event_emitter
method on : int -> unit
end
But I get a compiler error about a type signature mismatch between the two. I've also tried using the virtual
keyword, doing class type virtual event_emitter = method virtual on : string -> unit
but this also doesn't work as I guess these are class type definitions, not implementations anyway.
I really want this method override to work and this seemed straightforward, not sure why its not allowed.
Upvotes: 1
Views: 686
Reputation: 3739
This doesn't contradict antron's answer, but you can do this:
class type ['a] event_emitter = object
method on : 'a -> unit
end
class type socket = object
inherit [int] event_emitter
end
Upvotes: 2
Reputation: 3847
What you are trying to do is overloading, not overriding. You are trying to create a new method in socket
with the same name as in event_emitter
, but with a different type. Overriding would be creating a new method with the same name and type. This description would hold for other languages like Java and C++, as well. The difference is that I don't believe OCaml allows this kind of overloading – the same as for regular functions.
Note that if you entered the analogous declarations for Java, it would work, but socket
would have two on
methods, not one. OCaml doesn't allow this.
Upvotes: 2