caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Fred Smith" <fsmith@mathworks.com>
To: "Frederic Tronel" <Frederic.Tronel@inrialpes.fr>, <caml-list@inria.fr>
Subject: RE: [Caml-list] Polymorphic methods
Date: Fri, 23 Aug 2002 13:32:42 -0400	[thread overview]
Message-ID: <IIEMJEMIMDMLIIPHPOBLMEJACAAA.fsmith@mathworks.com> (raw)
In-Reply-To: <3D6658D0.F18A453D@inrialpes.fr>


Hi,

The method set_t would not be type safe given the type you have assigned it.
OK, the code snippet you wrote happens to be safe but won't be as soon as
you try to use it.

With the type you have assigned set_t the following would be legal:

n#set_t 3.4;
n#set_t "hello";

because set_t takes any type as input ('a.'a->unit).

What type would n#t have?  Neither float option nor string option will work.
In particular, if you had provided a function get_t it could not return a
useful single type.  To see the problem consider

if myWhackoCondition then (n#set_t 3.4) else (n#set_t "hello");
let w = n#get_t () in (* What type should w have? *)

A class definition that is legal and may solve your problem is:

class ['a] node = object
  val mutable t : 'a option = None
  method set_t nt = t<- Some nt
  method get_t () = (match t with Some x -> x | _ -> failwith "Empty node")
  method is_empty () = (t = None)
end

Now you can create values of type int node, float node, etc... and set_t and
get_t will return the appropriate types.  But you cannot create a value of
type 'a.'a node for the same reasons as above.

Hope this helps.

-Fred

> -----Original Message-----
> From: owner-caml-list@pauillac.inria.fr
> [mailto:owner-caml-list@pauillac.inria.fr]On Behalf Of Frederic Tronel
> Sent: Friday, August 23, 2002 11:46 AM
> To: caml-list@inria.fr
> Subject: [Caml-list] Polymorphic methods
>
>
> Hello list,
>
>
> I've just compiled the new 3.05 version (I will change for 3.06
> tomorrow),
> and start playing a little bit with polymorphic methods.
> But this small piece of code does not compile:
>
> class node =
> object
> val mutable t = None
> method set_t: 'a. 'a -> unit = fun nt -> t <- Some nt
> end
>
> File "essai.ml", line 4, characters 31-53:
> This method has type 'a -> unit which is less general than 'b. 'b ->
> unit
>
> while this one is OK (useless).
>
> class node =
> object
> val mutable t = None
> method set_t: 'a. 'a -> unit = fun nt -> ()
> end
> class node : object val mutable t : 'a option method set_t : 'b -> unit
> end
>
> Where am I going wrong ??
>
> Best regards,
>
> Frederic.
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives:
> http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ:
> http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2002-08-23 17:32 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-23 15:46 Frederic Tronel
2002-08-23 17:32 ` Fred Smith [this message]
2002-08-23 18:21 ` Remi VANICAT
  -- strict thread matches above, loose matches on Subject: below --
2015-12-18  9:18 [Caml-list] polymorphic methods Christoph Höger
2015-12-18 10:07 ` Leo White
2003-03-12 23:07 Damien
2003-03-13  0:56 ` brogoff
2003-03-13  1:56 ` Jacques Garrigue
2003-03-13  9:04   ` Damien
2003-03-13  9:27     ` Jacques Garrigue
2003-03-13 14:49       ` Damien
2003-03-13  9:41     ` Olivier Andrieu
2002-07-14 23:16 nadji
2002-07-15  1:05 ` Jacques Garrigue
2002-07-15  2:08   ` Brian Smith
2002-07-15 16:24   ` nadji
2001-11-19 15:29 [Caml-list] Polymorphic methods Alain Frisch
2001-11-20  0:29 ` Jacques Garrigue
2001-11-20  9:33   ` Alain Frisch
2001-11-20 20:55   ` Xavier Leroy

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=IIEMJEMIMDMLIIPHPOBLMEJACAAA.fsmith@mathworks.com \
    --to=fsmith@mathworks.com \
    --cc=Frederic.Tronel@inrialpes.fr \
    --cc=caml-list@inria.fr \
    /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).