caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Pierre-Alexandre Voye <ontologiae@gmail.com>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Re: Define class and sum type in one time
Date: Fri, 18 Mar 2011 18:16:59 +0100	[thread overview]
Message-ID: <1300468619.8429.8.camel@thinkpad> (raw)
In-Reply-To: <AANLkTikhTk9d9nQa3jCGaLgSBCeuQUCDM=zNp=LNS4Vs@mail.gmail.com>

Am Donnerstag, den 17.03.2011, 11:50 +0100 schrieb Pierre-Alexandre
Voye:
> So, this is impossible  ?   :
> 
> class maclasse = object(self)
>   val mutable valeur = (None : truc option)
>   method getValeur = valeur
> end
> and 
> truc = Machin of (maclasse->int) | Recur of truc;; 
> 
> ?
> (three attempt to write a correct example of my problem.. All
> apologies !)

This is the closest without introducing a type variable:

type maclasse_t = < getValeur : truc option >
and truc = Machin of maclasse_t->int | Recur of truc

Here, maclasse is just an object type (which is less than a full class
type, as it has only methods but no type information about instance
variables).

In a second step you can define the class:

class maclasse = ( object ... end : maclasse_t )

Generally, it is a good recommendation to define object or class types
before doing the class implementation. Not only allows it more
complicated types but it is also easier to get the typing right, because
you have always maclasse_t at hand to give type hints.

Gerd

> 
> 2011/3/17 Guillaume Yziquel <guillaume.yziquel@citycable.ch>
>         Le Thursday 17 Mar 2011 à 11:33:44 (+0100), Pierre-Alexandre
>         Voye a écrit :
>         >    My example would be better with this :
>         >    class machin = object(self)
>         
>         >    Â  val mutable valeur = (None : truc option)
>         >    Â  method getValeur = valeur
>         >    end
>         >    and
>         >    truc = Machin of (int->int) | Recur of truc;;
>         >    The object need the type defined
>         
>         
>         You cannot declare a class and a type with an 'and'. Either
>         two classes,
>         or two types.
>         
>         Simply declare truc before machin.
>         
>         type truc = Machin of int -> int | Recur of truc
>         
>         class machin = object
>         
>                val mutable valeur : truc option = None
>         
>                method getValeur = valeur
>         
>         end
>         
>         --
>             Guillaume Yziquel
> 
> 
> 
> -- 
> ---------------------
> Isaac Project - http://www.lisaac.org/


-- 
------------------------------------------------------------
Gerd Stolpmann, Bad Nauheimer Str.3, 64289 Darmstadt,Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714
------------------------------------------------------------



  parent reply	other threads:[~2011-03-18 17:17 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-17 10:08 [Caml-list] " Pierre-Alexandre Voye
2011-03-17 10:33 ` [Caml-list] " Pierre-Alexandre Voye
2011-03-17 10:47   ` Guillaume Yziquel
2011-03-17 10:50     ` Pierre-Alexandre Voye
2011-03-17 11:01       ` David Rajchenbach-Teller
2011-03-17 11:14         ` David Rajchenbach-Teller
2011-03-17 11:05       ` Guillaume Yziquel
2011-03-17 13:44         ` Gabriel Scherer
     [not found]         ` <1080649473.122478.1300369493351.JavaMail.root@zmbs4.inria.fr>
2011-03-17 13:54           ` Fabrice Le Fessant
2011-03-18 17:16       ` Gerd Stolpmann [this message]
     [not found]     ` <350217430.1223639.1300359057593.JavaMail.root@zmbs3.inria.fr>
2011-03-17 11:02       ` Paolo Herms

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=1300468619.8429.8.camel@thinkpad \
    --to=info@gerd-stolpmann.de \
    --cc=caml-list@inria.fr \
    --cc=ontologiae@gmail.com \
    /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).