"ocaml_beginners"::[] subtyping constraints for polymorphic types

View: New views
2 Messages — Rating Filter:   Alert me  

"ocaml_beginners"::[] subtyping constraints for polymorphic types

by Mirza Akbar Shah :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
I can't seem to find info on this in any ocaml reference, so I hope
somebody can help me out here. What I want to do is pretty simple:
I want to create a polymorphic class with a single polymorphic type 'a
where 'a has to be a subclass of another class called "base":

class base =
object
 method getAMethodOnlyBaseWouldHave = 4
end;;

class ['a] foo (baseInstance : 'a)  =
object (self : 'self)
   method getBaseInstance = baseInstance
   method doSomethingWithBaseInstance =
self#getBaseInstance#getAMethodOnlyBaseWouldHave
end;;

I want to constrain 'a further so that 'a is a subclass of base (i.e. 'a
<: base). If I use the code above, I get the following type info in the
toplevel:

class base : object method getAMethodOnlyBaseWouldHave : int end
class ['a] foo :
  'a ->
  object
    constraint 'a = < getAMethodOnlyBaseWouldHave : 'b; .. >
    method doSomethingWithBaseInstance : 'b
    method getBaseInstance : 'a
  end


This sort of does what I want because 'a has to be an object with a
method called getAMethodOnlyBaseWouldHave. However, that object does not
have to be of class type base, which is not quite what I would like. Any
ideas? Is this even possible in ocaml? Any help would be greatly
appreciated as I'm banging my head against the wall. Fyi, my background
in OO languages is more akin to languages like C++/Java/C#...so I may be
misunderstanding the concepts of class/object in ocaml.

Thanks for any help.






"ocaml_beginners"::[] Re: subtyping constraints for polymorphic types

by jshaw10 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Mirza,
I'm not sure if this is what you're looking for, but it looks promising. I found this in http://caml.inria.fr/pub/docs/manual-ocaml/types.html

"#-types

The type # class-path is a special kind of abbreviation. This abbreviation unifies with the type of any object belonging to a subclass of class class-path."

So maybe something like

val something : #base

forces "something" to belong to any class that is a subclass of "base"?

Hope this helps,
Jeff

--- In ocaml_beginners@..., Mirza Akbar Shah <mas644@...> wrote:

>
> Hi,
> I can't seem to find info on this in any ocaml reference, so I hope
> somebody can help me out here. What I want to do is pretty simple:
> I want to create a polymorphic class with a single polymorphic type 'a
> where 'a has to be a subclass of another class called "base":
>
> class base =
> object
>  method getAMethodOnlyBaseWouldHave = 4
> end;;
>
> class ['a] foo (baseInstance : 'a)  =
> object (self : 'self)
>    method getBaseInstance = baseInstance
>    method doSomethingWithBaseInstance =
> self#getBaseInstance#getAMethodOnlyBaseWouldHave
> end;;
>
> I want to constrain 'a further so that 'a is a subclass of base (i.e. 'a
> <: base). If I use the code above, I get the following type info in the
> toplevel:
>
> class base : object method getAMethodOnlyBaseWouldHave : int end
> class ['a] foo :
>   'a ->
>   object
>     constraint 'a = < getAMethodOnlyBaseWouldHave : 'b; .. >
>     method doSomethingWithBaseInstance : 'b
>     method getBaseInstance : 'a
>   end
>
>
> This sort of does what I want because 'a has to be an object with a
> method called getAMethodOnlyBaseWouldHave. However, that object does not
> have to be of class type base, which is not quite what I would like. Any
> ideas? Is this even possible in ocaml? Any help would be greatly
> appreciated as I'm banging my head against the wall. Fyi, my background
> in OO languages is more akin to languages like C++/Java/C#...so I may be
> misunderstanding the concepts of class/object in ocaml.
>
> Thanks for any help.
>