caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Alain Frisch <alain@frisch.fr>
To: Ivan <ivg@ieee.org>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] type inference with classes
Date: Fri, 03 Aug 2012 06:50:19 +0200	[thread overview]
Message-ID: <501B588B.6040006@frisch.fr> (raw)
In-Reply-To: <CAL9dnAFiQGxnUmyWsbeTvHz2rmUV9HUOwJmpHa3imrZK0Eri=A@mail.gmail.com>

Hello,

I don't know if this covers all the case, but you need to keep in mind 
the following points.  Feel free to add to the list.

1. The type-checker will never infer that a method is polymorphic.  You 
need to tell it.

  object(this)
     method f = (this # g 1, this # g true)
     method g x = x
  end

===>

   object(this)
     method f = (this # g 1, this # g true)
     method g: 'a. 'a -> 'a = fun x -> x
   end


2. Same for labeled and optional arguments on methods, when the type for 
the method to be called is not known.

   class virtual c =
     object(this)
       method f = this # g ~x:1 ~y:2 + this # g ~y:1 ~x:2
     end

===>

   class virtual c =
     object(this)
       method f = this # g ~x:1 ~y:2 + this # g ~y:1 ~x:2
       method virtual g: x:int -> y:int -> int
     end

(Note: this is really not specific to objects, a simple (fun g -> g ~x:1 
~y:2 + g ~y:1 ~x:2) has the same effect, but it's likely to hit you if 
you use virtual classes.)

3. When declaring classes, the inferred type must have no free variable. 
  So while the expression "object method f x = x end" is allowed and is 
inferred to have a type "< f : 'a -> 'a >", the following class 
declaration is rejected:

class c = object method f x = x end

Depending on what you want to do, you can either force a specific type 
or make the class parametric:

class c = object method f x : int = x end
class ['a] c = object method f x : 'a = x end


-- Alain




On 8/3/2012 6:23 AM, Ivan wrote:
> Hi!
>
> I've mentioned that in some cases ocaml can't infer type (or maybe it
> can, but didn't want to...) of some objects.
> Right to this moment, I've just do as ocaml wants - annotate a type and
> move forward. But now I want to make things more consciously. The reason
> is simple - sometimes ocaml infers types correctly and denotes a type
> error in my code, but I, mistakenly thinking that it can't infer type of
> object, loose my time in useless type annotating of different
> identifiers. Or vice versa, the code is correct, except for some
> undecidable object type, and I spend h^Wminutes in checking my code for
> errors.
>
> So, I'would like to know the rules of type inference failures with
> object types. In what cases an object type can and must be inferred, and
> in what it must be annotated explicitly?
>
> Can someone share this arcane knowledge or, at least, point me at some
> sources, explaining this issue?
>
> Thanks everybody in advance!


  reply	other threads:[~2012-08-03  4:50 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-03  4:23 Ivan
2012-08-03  4:50 ` Alain Frisch [this message]
2012-08-03  5:15   ` Ivan
2012-08-03  5:32     ` Jacques Garrigue
2012-08-03  6:09       ` Ivan
2012-08-03 23:14 Jacques Garrigue

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=501B588B.6040006@frisch.fr \
    --to=alain@frisch.fr \
    --cc=caml-list@inria.fr \
    --cc=ivg@ieee.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).