Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error using inherit, virtual and polymorphic class

0 views
Skip to first unread message

tmp123

unread,
Oct 31, 2006, 7:04:32 AM10/31/06
to
Hello,

Thanks for your time.

The small code at the end of this post causes an intepreter error.
After review several documentation, I can not find the origin of it,
even when it should be something very easy. In addition, I've several
doubts:

1) Why the error

2) Should be:
class virtual ['a] lst () =
or
class virtual ['a] lst =

3) Should I write "object ( _ : 'b)" and then use "'b" instead of "'a
lst", in the paramters and results declarations?

Please, any sugestion is welcome.

Thanks.

class virtual ['a] lst () =
object
method virtual cons : 'a -> 'a lst
method virtual hq : unit -> ('a*'a lst) option
end;;

!class virtual ['a] lst :
! unit ->
! object
! method virtual cons : 'a -> 'a lst
! method virtual hq : unit -> ('a * 'a lst) option
! end

class ['a] olst (pl:'a list) =
object
inherit ['a] lst ()
val l = pl
method cons (v:'a) =
new olst (v::l)
method hq () =
match l with
[] -> None
| h::q -> Some(h,new olst q)
method to_list () = l
end;;

!The expression "new olst" has type 'a list -> 'a olst but is used with
type
! 'a list -> 'a lst
!Type
! 'a olst =
! < cons : 'a -> 'a lst; hq : unit -> ('a * 'a lst) option;
! to_list : unit -> 'a list >
!is not compatible with type
! 'a lst = < cons : 'a -> 'a lst; hq : unit -> ('a * 'a lst) option >
!Only the first object type has a method to_list

tmp123

unread,
Oct 31, 2006, 8:41:21 AM10/31/06
to
A second try, using "object(self:'o)", seems a few more correct, but
with error.

The error "self type cannot escape its class" is documented in the
manual and in the www, but for the moment, I can not overpass it:


class virtual ['a] lst () =

object (self:'o)
method virtual cons : 'a -> 'o
method virtual hq : unit -> ('a*'o) option
end;;

! class virtual ['a] lst :
! unit ->
! object ('b)
! method virtual cons : 'a -> 'b
! method virtual hq : unit -> ('a * 'b) option
! end

class ['a] olst (pl:'a list) =

object(self:'o)
inherit ['a] lst ()
val l : 'a list = pl
method cons (v:'a) : 'o =
new olst (v::l)
method hq () : ('a*'o) option =


match l with
[] -> None
| h::q -> Some(h,new olst q)

method to_list () : a' list = l
end;;

! This expression has type 'a but is here used with type
! < cons : 'c -> 'b; hq : unit -> ('c * 'b) option; to_list : 'd ->
'e; .. > a
s 'b
! Self type cannot escape its class

tmp123

unread,
Oct 31, 2006, 9:21:09 AM10/31/06
to
Oops, found: it seems to use "new" in a method is not a good idea (for
the same class). Replace "new" by {< xxx >} expressions.

Final version, if nobody has comments:

class virtual ['a] lst () =
object (self:'o)
method virtual cons : 'a -> 'o
method virtual hq : unit -> ('a*'o) option
end;;

class ['a] olst (pl:'a list) =
object
inherit ['a] lst ()
val l = pl
method cons (v:'a) = {< l = v::l >}
method hq () =


match l with
[] -> None

| h::q -> Some(h,{<l=q>})


method to_list () = l
end;;

Thanks for your time.

0 new messages