caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Utopian idea ?
@ 2011-09-16 21:33 Pierre-Alexandre Voye
  2011-09-26 19:13 ` Hezekiah M. Carty
  0 siblings, 1 reply; 2+ messages in thread
From: Pierre-Alexandre Voye @ 2011-09-16 21:33 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 2019 bytes --]

Hello,

i'm writting an object which implements a directory service. The goal is to
be able to add dynamically directories.
My directory links IDs to different type of object.
Thus the object would contain several hashtables where the key is a string
(the ID), and the value an object.
Of course, each hashtable would contain only a type.
For instance, the first hashtable is the directory of object "ant", the
second "cheese", etc...
An "index" hashtable allow to register a new hashtable for a new type

I implemented the directory, and as i expected i had a problem : the
compiler doesn't accept it because it doesn't know the type of object.

So here the code :

class _DIRECTORY = object  (self :'self)
    val mutable indexDico = Hashtbl.create 128
    val mutable dictionnaires =  Hashtbl.create 128
    val mutable maxIndex = 0
    method getDict = dictionnaires
    method getIndex = indexDico
    method getMaxIndex = maxIndex
    method incMaxIndex = maxIndex <- (self#getMaxIndex +1)
    method addDict (name:string) = (self#incMaxIndex) ;
                           let elem = Hashtbl.create 128 in Hashtbl.add
(self#getIndex) name elem
  end;;

Error: Some type variables are unbound in this type:
         class _DIRECTORY :
           object
             val mutable dictionnaires : ('a, 'b) Hashtbl.t
             val mutable indexDico : (string, ('c, 'd) Hashtbl.t) Hashtbl.t
             val mutable maxIndex : int
             method addDict : string -> unit
             method getDict : ('a, 'b) Hashtbl.t
             method getIndex : (string, ('c, 'd) Hashtbl.t) Hashtbl.t
             method getMaxIndex : int
             method incMaxIndex : unit
           end
       The method getDict has type ('a, 'b) Hashtbl.t where 'b is unbound


It doen't surprised me anymore, but i there a strategy to allow that and to
get round this difficulties ?

Thank you for your ideas.

Regards,

Pierre-Alex

-- 
---------------------
https://twitter.com/#!/ontologiae/
http://linuxfr.org/users/montaigne

[-- Attachment #2: Type: text/html, Size: 2645 bytes --]

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

* Re: [Caml-list] Utopian idea ?
  2011-09-16 21:33 [Caml-list] Utopian idea ? Pierre-Alexandre Voye
@ 2011-09-26 19:13 ` Hezekiah M. Carty
  0 siblings, 0 replies; 2+ messages in thread
From: Hezekiah M. Carty @ 2011-09-26 19:13 UTC (permalink / raw)
  To: Pierre-Alexandre Voye; +Cc: caml-list

On Fri, Sep 16, 2011 at 5:33 PM, Pierre-Alexandre Voye
<ontologiae@gmail.com> wrote:
>
> I implemented the directory, and as i expected i had a problem : the
> compiler doesn't accept it because it doesn't know the type of object.
>
> So here the code :
>
> class _DIRECTORY = object  (self :'self)
>     val mutable indexDico = Hashtbl.create 128
>     val mutable dictionnaires =  Hashtbl.create 128
>     val mutable maxIndex = 0
>     method getDict = dictionnaires
>     method getIndex = indexDico
>     method getMaxIndex = maxIndex
>     method incMaxIndex = maxIndex <- (self#getMaxIndex +1)
>     method addDict (name:string) = (self#incMaxIndex) ;
>                            let elem = Hashtbl.create 128 in Hashtbl.add
> (self#getIndex) name elem
>   end;;
>
> Error: Some type variables are unbound in this type:
>          class _DIRECTORY :
>            object
>              val mutable dictionnaires : ('a, 'b) Hashtbl.t
>              val mutable indexDico : (string, ('c, 'd) Hashtbl.t) Hashtbl.t
>              val mutable maxIndex : int
>              method addDict : string -> unit
>              method getDict : ('a, 'b) Hashtbl.t
>              method getIndex : (string, ('c, 'd) Hashtbl.t) Hashtbl.t
>              method getMaxIndex : int
>              method incMaxIndex : unit
>            end
>        The method getDict has type ('a, 'b) Hashtbl.t where 'b is unbound
>
>
> It doen't surprised me anymore, but i there a strategy to allow that and to
> get round this difficulties ?
>

You need to provide some explicit types:

class ['a, 'b] _DIRECTORY = object  (self :'self)
  val mutable indexDico : ('a, 'b) Hashtbl.t = Hashtbl.create 128
  val mutable dictionnaires : ('a, 'b) Hashtbl.t =  Hashtbl.create 128
  val mutable maxIndex = 0
  method getDict = dictionnaires
  method getIndex = indexDico
  method getMaxIndex = maxIndex
  method incMaxIndex = maxIndex <- (self#getMaxIndex +1)
  method addDict (name:string) =
    (self#incMaxIndex) ;
    let elem = Hashtbl.create 128 in Hashtbl.add (self#getIndex) name
elem
end

See the class section of the manual for more information:

http://caml.inria.fr/pub/docs/manual-ocaml/manual005.html#toc27

I hope this helps.

Hez


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

end of thread, other threads:[~2011-09-26 19:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-16 21:33 [Caml-list] Utopian idea ? Pierre-Alexandre Voye
2011-09-26 19:13 ` Hezekiah M. Carty

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