Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: tim@fungible.com (Tim Freeman)
To: caml-list@inria.fr, caml-bugs@inria.fr
Subject: [Caml-list] Allow declaring classes "final" so self type has no type variables?
Date: Fri,  6 Sep 2002 12:12:42 -0700	[thread overview]
Message-ID: <20020906192333.1663A7F9A@lobus.fungible.com> (raw)


I find that when I'm writing types of objects, there are a lot more
type variables running around than I need.  The fundamental cause of
this appears to be that it is possible to inherit from any class, so
the type of "self" for an object is never constant; it always has to
be a type variable just in case the present object is a subclass of
the object that's presently being defined.

In practice, it seems that many of the classes I write will never have
a subclass and these type variables are unnecessary.  I'd like to be
able to declare that a class will never have a subclass, and then the
compiler will be willing to unify constant types with the type of
self.  In this case, of course, any attempt to inherit the class
should get an error.

Here's some sample code illustrating the situation.  The comment at
the end shows how I'd like to be able to write the last class.

I suspect method invocation on a final class could be more efficient
than method invocation in the general case, too.

Any comments on whether this is a good idea?

-- 
Tim Freeman       
tim@fungible.com
GPG public key fingerprint ECDF 46F8 3B80 BB9E 575D  7180 76DF FE00 34B1 5C78 

(* The real hierarchy_node is exported by a package that displays trees of
   nodes in general.  The type variables are necessary. *)
class type hierarchy_node = object ('node)
  method children: 'node array
end

(* I can believe the type variables are necessary here, too. *)
class type nodetype =
object ('node)
  constraint 'node = #hierarchy_node
  method children: 'node array
  method add_child: 'node -> unit
  method parent: 'node option
end

(* I omitted a bunch of code here that uses nodetype (above) and is used by
   omitted methods on node (below).  This is why I have to separate the
   declaration of nodetype from the declaration of node. *)

(* I don't think the type variables in the definition of node make anything
   better at all.  Fundamentally, node has to be polymorphic when considering
   its own class so we can cope with taking a subclass of node.
   But I know that I don't want to take any subclasses, so I'd rather not have
   the polymorphism.  I'd like the option of telling the compiler this so I
   could avoid type variables I don't need. *)

class node ~(parent: 'node option) =
object (self: 'node)
  constraint 'node = #nodetype
                      
  val mutable children: 'node array = [||]

  initializer
    match parent with
        None -> ()
      | Some p -> p#add_child self

  method children: 'node array = children

  method add_child (n: 'node): unit =
    assert (n#parent = Some self);
    for i = 0 to Array.length children - 1 do
      assert (n != children.(i));
    done;
    children <- Array.append children [|n|]

  method parent: 'node option = parent
end

(* I propose that the way to tell the compiler that the class will never have a
   subclass is by using the new keyword "final", as in Java.  Then I could
   write something like this instead:

class final node ~(parent: node option): nodetype =
object self
  val mutable children: node array = [||]

  initializer
    match parent with
        None -> ()
      | Some p -> p#add_child self

  method children: node array = children

  method add_child (n: node): unit =
    assert (n#parent = Some self);
    for i = 0 to Array.length children - 1 do
      assert (n != children.(i));
    done;
    children <- Array.append children [|n|]

  method parent: node option = parent
end
*)
-------------------
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-09-06 19:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-09-06 19:12 Tim Freeman [this message]
2002-09-06 20:18 ` james woodyatt
2002-09-06 20:38   ` Alessandro Baretta
2002-09-06 21:15     ` james woodyatt
2002-09-11  0:16       ` Tim Freeman
2002-09-11  5:09         ` james woodyatt

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=20020906192333.1663A7F9A@lobus.fungible.com \
    --to=tim@fungible.com \
    --cc=caml-bugs@inria.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