caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] A class that contains instances of itself?
@ 2002-01-05 12:45 wrader
  2002-01-05 13:15 ` John Prevost
  2002-01-05 13:16 ` Alain Frisch
  0 siblings, 2 replies; 4+ messages in thread
From: wrader @ 2002-01-05 12:45 UTC (permalink / raw)
  To: caml-list

Please forgive me if there is an obvious answer to
my question.

I want to have an class that can contain instances
of that same class.  Assume something like this:
(ignore any easy syntax errors - I'm making
up this example right now)

class someClass = 
  object
    var mutable anotherInstanceOfSomeClass ???????

    method talk () =
      print_string("Hello.\n")

    method setInstance x = 
      anotherInstanceOfSomeClass <- x

    method useInstance () =
      if anotherInstanceOfSomeClass is set to a
      usable value, then do
        anotherInstanceOfSomeClass#talk
      else
        do nothing
  end;;


One must call anotherInstanceOfSomeClass#setInstance
before using anotherInstanceOfSomeClass#talk.

What do I put in place of the ?????? to indicate
that the value is not yet usable?  I could create
a "dummy" someClass object to use as the "NULL" value,
let's say called someClassNULL.  someClassNULL must
already be defined if I use it in the body of
someClass, but I can't create it before the
definition of someClass because the compiler doesn't
know what someClass _is_ yet.  Catch-22!!

I've been staring at this problem for hours, so any
help would be fantastic!  (If only I spoke French and
could read the many books available about OCaml...)

Thanks,

Walter Rader
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] A class that contains instances of itself?
  2002-01-05 12:45 [Caml-list] A class that contains instances of itself? wrader
@ 2002-01-05 13:15 ` John Prevost
  2002-01-05 13:16 ` Alain Frisch
  1 sibling, 0 replies; 4+ messages in thread
From: John Prevost @ 2002-01-05 13:15 UTC (permalink / raw)
  To: wrader; +Cc: caml-list

On Sat, Jan 05, 2002 at 04:45:59AM -0800, wrader@OCF.Berkeley.EDU wrote:

>class someClass = 
>  object
>    var mutable anotherInstanceOfSomeClass ???????
>
>    method talk () =
>      print_string("Hello.\n")
>
>    method setInstance x = 
>      anotherInstanceOfSomeClass <- x
>
>    method useInstance () =
>      if anotherInstanceOfSomeClass is set to a
>      usable value, then do
>        anotherInstanceOfSomeClass#talk
>      else
>        do nothing
>  end;;
>
>
>One must call anotherInstanceOfSomeClass#setInstance
>before using anotherInstanceOfSomeClass#talk.
>
>What do I put in place of the ?????? to indicate
>that the value is not yet usable?  I could create
>a "dummy" someClass object to use as the "NULL" value,
>let's say called someClassNULL.  someClassNULL must
>already be defined if I use it in the body of
>someClass, but I can't create it before the
>definition of someClass because the compiler doesn't
>know what someClass _is_ yet.  Catch-22!!
>
>I've been staring at this problem for hours, so any
>help would be fantastic!  (If only I spoke French and
>could read the many books available about OCaml...)

In this case, I would write:

  object ('s)
    val mutable anotherInstanceOfSomeClass = (ref None : 's option)

    method talk () =
      print_string("Hello.\n")

    method setInstance x =
      anotherInstanceOfSomeClass <- Some x

    method useInstance () =
      match anotherInstanceOfSomeClass
        | Some x -> anotherInstanceOfSomeClass#talk ()
        | None -> ()
  end

The ('s) part binds the "self type" of the class so you can refer
to it.  "foo option" is either "None" or "Some x" where x is a
value of type foo.  The option type is the wonderful way that
languages with reasonably powerful type systems allow us to add
something like "NULL" only where we want it, instead of having it
everywhere.

Another way of looking at things (in this case) is that you only
need some object with a talk method in that value--not necessarily
the same type object as your class, so you could do:

class doNothing =
  object
    method talk () = ()
  end

and then use an instance of that as a placeholder.  But, this will
lead to other typing issues, as you'll have to cast everything to
only include the talk method before you pass them in.  (Ignore this
for now if it doesn't make sense to you, and read through all of
the object examples in the manual.)

Do read the O'Caml manual.  It's written in English, and while it
doesn't recommend ways of doing everything like this, actually
reading through it from end to end can suggest a lot of ways to do
things.

John.
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] A class that contains instances of itself?
  2002-01-05 12:45 [Caml-list] A class that contains instances of itself? wrader
  2002-01-05 13:15 ` John Prevost
@ 2002-01-05 13:16 ` Alain Frisch
  2002-01-05 23:27   ` [Caml-list] IMAP library for Caml? Will Benton
  1 sibling, 1 reply; 4+ messages in thread
From: Alain Frisch @ 2002-01-05 13:16 UTC (permalink / raw)
  To: wrader; +Cc: caml-list

On Sat, 5 Jan 2002 wrader@ocf.berkeley.edu wrote:

> I want to have an class that can contain instances
> of that same class.  Assume something like this:
> (ignore any easy syntax errors - I'm making
> up this example right now)
...

What about :

class someClass =
object(_ : 'self)
  val mutable anotherInstanceOfSomeClass : 'self option = None

  method talk () =
    print_string("Hello.\n")

  method setInstance x =
    anotherInstanceOfSomeClass <- Some x

  method useInstance () =
    match anotherInstanceOfSomeClass with
      | Some a -> a # talk ()
      | None -> ()
end


?

-- 
  Alain

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* [Caml-list] IMAP library for Caml?
  2002-01-05 13:16 ` Alain Frisch
@ 2002-01-05 23:27   ` Will Benton
  0 siblings, 0 replies; 4+ messages in thread
From: Will Benton @ 2002-01-05 23:27 UTC (permalink / raw)
  To: caml-list

Hello,

Does anyone know of a (LGPL/GPL/BSD) IMAP client library for Caml?



thanks,
wb

---
Will Benton      | "Nicht wie die Welt ist, ist das Mystische, 
willb@acm.org    |  sondern daß sie ist." --L. Wittgenstein

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2002-01-07  9:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-01-05 12:45 [Caml-list] A class that contains instances of itself? wrader
2002-01-05 13:15 ` John Prevost
2002-01-05 13:16 ` Alain Frisch
2002-01-05 23:27   ` [Caml-list] IMAP library for Caml? Will Benton

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