caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] ocaml, objects, classes, type ascription, virtual methods
@ 2011-05-23  7:50 Dmitry Grebeniuk
  2011-05-23  8:29 ` Matthieu Dubuget
  2011-05-23 10:11 ` Jacques Garrigue
  0 siblings, 2 replies; 7+ messages in thread
From: Dmitry Grebeniuk @ 2011-05-23  7:50 UTC (permalink / raw)
  To: caml-list

Hello.

  I have a base class with virtual methods and an extended classes
where these methods are implemented.
  When I'm ascribing type "base" to class "ext", I get an error:

=============
        OCaml version 3.13.0+dev3 (2011-03-07)

# class virtual base = object method virtual m : unit end;;
class virtual base : object method virtual m : unit end
# class ext : base = object method m = () end;;
Characters 6-43:
  class ext : base = object method m = () end;;
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This class should be virtual. The following methods are undefined : m
#
=============

  Is it the expected behaviour?
  It would be more useful to allow to constrain "class ext" by type "base"
while providing an implementation for virtual methods.
  For now, I'm omitting the type ascription, but then I have less guarantees
about extended classes' compatibility/interchangeability.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] ocaml, objects, classes, type ascription, virtual methods
  2011-05-23  7:50 [Caml-list] ocaml, objects, classes, type ascription, virtual methods Dmitry Grebeniuk
@ 2011-05-23  8:29 ` Matthieu Dubuget
  2011-05-23  8:40   ` Dmitry Grebeniuk
  2011-05-23 10:11 ` Jacques Garrigue
  1 sibling, 1 reply; 7+ messages in thread
From: Matthieu Dubuget @ 2011-05-23  8:29 UTC (permalink / raw)
  To: Caml Mailing List

I'm not sure I understand your question.

Isn't "inherit" what you want?

Salutations

Matt



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] ocaml, objects, classes, type ascription, virtual methods
  2011-05-23  8:29 ` Matthieu Dubuget
@ 2011-05-23  8:40   ` Dmitry Grebeniuk
  2011-05-23  9:08     ` Lauri Alanko
  0 siblings, 1 reply; 7+ messages in thread
From: Dmitry Grebeniuk @ 2011-05-23  8:40 UTC (permalink / raw)
  To: caml-list

Hello.

> I'm not sure I understand your question.
>
> Isn't "inherit" what you want?

  I don't need to inherit the methods (the class' behaviour),
I only need to be sure that types of methods are equal
(more precisely, that types of methods of class ext
can be constrained by types of methods of class base).

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] ocaml, objects, classes, type ascription, virtual methods
  2011-05-23  8:40   ` Dmitry Grebeniuk
@ 2011-05-23  9:08     ` Lauri Alanko
  2011-05-23  9:21       ` Dmitry Grebeniuk
  0 siblings, 1 reply; 7+ messages in thread
From: Lauri Alanko @ 2011-05-23  9:08 UTC (permalink / raw)
  To: caml-list

On Mon, May 23, 2011 at 11:40:00AM +0300, Dmitry Grebeniuk wrote:
>   I don't need to inherit the methods (the class' behaviour),
> I only need to be sure that types of methods are equal
> (more precisely, that types of methods of class ext
> can be constrained by types of methods of class base).

Are you sure you need a superclass at all? If you just want to make
sure that certain objects have certain methods, why not simply use
object types in signatures or type annotations?

type base = < m : unit >
class ext = object method m = () end
(new ext : base)


Lauri

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] ocaml, objects, classes, type ascription, virtual methods
  2011-05-23  9:08     ` Lauri Alanko
@ 2011-05-23  9:21       ` Dmitry Grebeniuk
  0 siblings, 0 replies; 7+ messages in thread
From: Dmitry Grebeniuk @ 2011-05-23  9:21 UTC (permalink / raw)
  To: caml-list

Hello.

> Are you sure you need a superclass at all? If you just want to make
> sure that certain objects have certain methods, why not simply use
> object types in signatures or type annotations?

  I want to avoid copy-paste required to write explicitely the
type of "base".  However, if this behaviour is normal/intended,
I will write the type "base" later.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] ocaml, objects, classes, type ascription, virtual methods
  2011-05-23  7:50 [Caml-list] ocaml, objects, classes, type ascription, virtual methods Dmitry Grebeniuk
  2011-05-23  8:29 ` Matthieu Dubuget
@ 2011-05-23 10:11 ` Jacques Garrigue
  2011-05-23 10:25   ` Dmitry Grebeniuk
  1 sibling, 1 reply; 7+ messages in thread
From: Jacques Garrigue @ 2011-05-23 10:11 UTC (permalink / raw)
  To: Dmitry Grebeniuk; +Cc: caml-list

On 2011/05/23, at 16:50, Dmitry Grebeniuk wrote:

> Hello.
> 
>  I have a base class with virtual methods and an extended classes
> where these methods are implemented.
>  When I'm ascribing type "base" to class "ext", I get an error:
> 
> =============
>        OCaml version 3.13.0+dev3 (2011-03-07)
> 
> # class virtual base = object method virtual m : unit end;;
> class virtual base : object method virtual m : unit end
> # class ext : base = object method m = () end;;
> Characters 6-43:
>  class ext : base = object method m = () end;;
>        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> Error: This class should be virtual. The following methods are undefined : m
> #
> =============
> 
>  Is it the expected behaviour?

It is indeed the expected behaviour.
Like a module signature, a class signature provides complete information.
If you declare a method as virtual in the signature, it will be made virtual by
the ascription (this is allowed, since virtual is a "supertype" of concrete).

In such a case you should rather use a class type:

  class type base = object method m : unit end

As suggested by others, if you don't want to suggest such a class
type you can also use the derived object type:

  class ext = object (_ : #base) method m = () end

These different approaches are described in the manual.

Jacques Garrigue

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] ocaml, objects, classes, type ascription, virtual methods
  2011-05-23 10:11 ` Jacques Garrigue
@ 2011-05-23 10:25   ` Dmitry Grebeniuk
  0 siblings, 0 replies; 7+ messages in thread
From: Dmitry Grebeniuk @ 2011-05-23 10:25 UTC (permalink / raw)
  To: caml-list

Hello.

>>  Is it the expected behaviour?
>
> It is indeed the expected behaviour.
[...]
>  class ext = object (_ : #base) method m = () end

  Thank you for the answer.  I'll choose this option --
it types, it works, it does not require explicit class type.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-05-23 10:25 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-23  7:50 [Caml-list] ocaml, objects, classes, type ascription, virtual methods Dmitry Grebeniuk
2011-05-23  8:29 ` Matthieu Dubuget
2011-05-23  8:40   ` Dmitry Grebeniuk
2011-05-23  9:08     ` Lauri Alanko
2011-05-23  9:21       ` Dmitry Grebeniuk
2011-05-23 10:11 ` Jacques Garrigue
2011-05-23 10:25   ` Dmitry Grebeniuk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).