caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] How to indicate that I am overriding an inherited virtual method?
@ 2017-01-13  9:09 François Pottier
       [not found] ` <CADDcBWXDbGyntNnv22Z4hv5K98OXu21mxS2d8Ju7k8ygqAmaQQ@mail.gmail.com>
  0 siblings, 1 reply; 2+ messages in thread
From: François Pottier @ 2017-01-13  9:09 UTC (permalink / raw)
  To: caml users


Dear OCaml users,

Is there an OCaml equivalent of Java's @Override keyword, which allows
indicating that a method definition is intended to override an inherited
(virtual or concrete) method?

As far as I can see, in OCaml, the "method!" form works for inherited 
concrete
methods:

   class base = object
     method foo: string = "uh?"
   end

   class child = object
     inherit base
     method! foo = "ha!"
       (* OK *)
   end

but does not work for inherited virtual methods:

   class virtual base = object
     method virtual foo: string
   end

   class child = object
     inherit base
     method! foo = "ha!"
       (* Error: The method `foo' has no previous definition *)
   end

Is this intentional?

This is too bad. I seem to be forced to use "method" instead of "method!".
Therefore, I cannot indicate my intent, and if I mistype the name "foo" in
the definition of the child class, the compiler cannot catch this mistake.
(Well, in this case, it can, as the child class is not declared virtual.)

--
François Pottier
francois.pottier@inria.fr
http://gallium.inria.fr/~fpottier/

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

* Re: [Caml-list] How to indicate that I am overriding an inherited virtual method?
       [not found] ` <CADDcBWXDbGyntNnv22Z4hv5K98OXu21mxS2d8Ju7k8ygqAmaQQ@mail.gmail.com>
@ 2017-01-13 11:04   ` François Pottier
  0 siblings, 0 replies; 2+ messages in thread
From: François Pottier @ 2017-01-13 11:04 UTC (permalink / raw)
  To: Nathan Moreau; +Cc: caml users

On 13/01/2017 10:20, Nathan Moreau wrote:
> You can ensure you are overriding, using
>
>   class virtual base = object
>     method virtual foo: string
>   end
>
>   class child = object
>     inherit base as b
>     method foo = if false then b # foo else "ha!"
>       (* OK *)
>   end

Interesting trick (albeit somewhat ugly), thanks!

-- 
François Pottier
francois.pottier@inria.fr
http://gallium.inria.fr/~fpottier/

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

end of thread, other threads:[~2017-01-13 11:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-13  9:09 [Caml-list] How to indicate that I am overriding an inherited virtual method? François Pottier
     [not found] ` <CADDcBWXDbGyntNnv22Z4hv5K98OXu21mxS2d8Ju7k8ygqAmaQQ@mail.gmail.com>
2017-01-13 11:04   ` François Pottier

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).