From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id D1F78800BE for ; Fri, 10 Feb 2017 04:32:40 +0100 (CET) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=yminsky@janestreet.com; spf=Pass smtp.mailfrom=yminsky@janestreet.com; spf=None smtp.helo=postmaster@mxout1.mail.janestreet.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of yminsky@janestreet.com) identity=pra; client-ip=38.105.200.78; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yminsky@janestreet.com"; x-sender="yminsky@janestreet.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of yminsky@janestreet.com designates 38.105.200.78 as permitted sender) identity=mailfrom; client-ip=38.105.200.78; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yminsky@janestreet.com"; x-sender="yminsky@janestreet.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mxout1.mail.janestreet.com) identity=helo; client-ip=38.105.200.78; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yminsky@janestreet.com"; x-sender="postmaster@mxout1.mail.janestreet.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3AlqTGHhJGw2nIHe3KF9mcpTZWNBhigK39O0sv0rFi?= =?us-ascii?q?tYgeIvrxwZ3uMQTl6Ol3ixeRBMOAuq4C27Sd7v+ocFdDyK7JiGoFfp1IWk1Nou?= =?us-ascii?q?QttCtkPvS4D1bmJuXhdS0wEZcKflZk+3amLRodQ56mNBXdrXKo8DEdBAj0OxZr?= =?us-ascii?q?KeTpAI7SiNm82/yv95HJbQhFgDWwbalxIRi3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us-ascii?q?4x6D82DsqdCIDHRYnl1KeE2yO4F7VdfSZNDUqKHHPsMYiIRqFfRjiVJ5pQmyYD?= =?us-ascii?q?U/CESok61gDm4B7/yrx8Lfv8+CQeuIn/ztV4+6vYkhRkpm88NNiUz2zYFzI8pW?= =?us-ascii?q?gPXTJjmfkn+UE=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0B2AQBmM51YfU7IaSZeDg0BAQEDAQEBC?= =?us-ascii?q?QEBARYBAQEDAQEBCQEBAYQHMVgHg1KKepEZiAyHeYUxgg0mhXwCgmUHQRYBAQE?= =?us-ascii?q?BAQEBAQEBARIBAQkWCkyCMxsBghsBBSMEGQEBEyQBDwsLDQICERUCAiEBEgEFA?= =?us-ascii?q?RwGEwkSiUEDFQMLoy4/ixpogWs6gwgBAQWELg2EDgEBAQEBAQEDAQEBAQEBAQE?= =?us-ascii?q?BFwgSeYVBhG+CUYIPM4JHgl+JEYc1inc6iDGBXoNsA4QWkQWKNYcZFB6BFSYFg?= =?us-ascii?q?SofEwgVFTwXBYQZDx2BJllXAYl6AQEB?= X-IPAS-Result: =?us-ascii?q?A0B2AQBmM51YfU7IaSZeDg0BAQEDAQEBCQEBARYBAQEDAQE?= =?us-ascii?q?BCQEBAYQHMVgHg1KKepEZiAyHeYUxgg0mhXwCgmUHQRYBAQEBAQEBAQEBARIBA?= =?us-ascii?q?QkWCkyCMxsBghsBBSMEGQEBEyQBDwsLDQICERUCAiEBEgEFARwGEwkSiUEDFQM?= =?us-ascii?q?Loy4/ixpogWs6gwgBAQWELg2EDgEBAQEBAQEDAQEBAQEBAQEBFwgSeYVBhG+CU?= =?us-ascii?q?YIPM4JHgl+JEYc1inc6iDGBXoNsA4QWkQWKNYcZFB6BFSYFgSofEwgVFTwXBYQ?= =?us-ascii?q?ZDx2BJllXAYl6AQEB?= X-IronPort-AV: E=Sophos;i="5.35,138,1484002800"; d="scan'208";a="212714775" Received: from mxout1.mail.janestreet.com ([38.105.200.78]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Feb 2017 04:32:39 +0100 Received: from tot-qpr-mailcore2.delacy.com ([172.27.56.106] helo=tot-qpr-mailcore2) by mxout1.mail.janestreet.com with esmtps (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cc1wr-0002S6-Rf for caml-list@inria.fr; Thu, 09 Feb 2017 22:32:37 -0500 X-JS-Flow: external X-JS-Scanner-attachment: (ok) No attachments Received: by tot-qpr-mailcore2 with JS-mailcore (0.1) (envelope-from ) id BYnTRV-AAADg1-Ya; 2017-02-09 22:32:37.782989-05:00 Received: from mail-vk0-f71.google.com ([209.85.213.71]) by mxgoog1.mail.janestreet.com with esmtps (TLSv1.2:AES128-GCM-SHA256:128) (Exim 4.84_2) (envelope-from ) id 1cc1wr-0007jj-NR for caml-list@inria.fr; Thu, 09 Feb 2017 22:32:37 -0500 Received: by mail-vk0-f71.google.com with SMTP id r136so13123046vke.6 for ; Thu, 09 Feb 2017 19:32:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=janestreet.com; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=Mm6Oy2rlfOR2ePaih2xVUpWKXxk0hgyeytr5mPI8OYU=; b=XLBsWX5/J+3EczvStzeBKQvX2G3YodbUJoylYoZ/IXLTdL4upVS9KTxhFh8Bnv+9+u VSJw3EOAoy/PuRGxdIkOAEwJ9v426g7htyMOk3RcAz92Hbks5KKhC5ULGrUcxtEc6e9T 8aoR8wcDFSJQnmv/9THXt9XIafPYldpBfiezI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=Mm6Oy2rlfOR2ePaih2xVUpWKXxk0hgyeytr5mPI8OYU=; b=ald+zyrZfwYP2033jJcyiFNft/5gUCou0vhNLqW3tinCCplHgwDBMH3kJf/OFWkdRQ 1UK1GOvwk9DRCOjCajKvCn1emaCGoiFKDKyOLTfNn8NabTMBee9QTMhvHH12AWl1seOq SEIwbV8CBJIFiR41v3oPryE/jvSotMXkoWPpNw8TAI9EVr9Ek0WeysdSQWIiYTd/Zevs lw326qdq71ZQ1rVXtAS/v9q3v5pag2oofTYROI2Z1kci8y9JcrJ085ZbzP8dEv6cpO1i SOVPEgQQOPG/zkUuBulgc16ZWzsKvQDgNzaAdpOy8foG/fefH2kVpbH/CSBE+dgHf23E TIkw== X-Gm-Message-State: AMke39k08kZgpVsDOi0VSPGN0xeXgwdeWtjIHyqTY3yL8lr7KHzlFCecoi5q4nf4y8i/6l/sLg4mMC/DLTxoX7kwQN5/7YW3DP9XP+jRytjx5jMXTDIFFK8DR3mqrfasox2/LNqP3fcFzyPRigmx X-Received: by 10.31.21.84 with SMTP id 81mr3367208vkv.17.1486697557194; Thu, 09 Feb 2017 19:32:37 -0800 (PST) X-Received: by 10.31.21.84 with SMTP id 81mr3367193vkv.17.1486697556746; Thu, 09 Feb 2017 19:32:36 -0800 (PST) MIME-Version: 1.0 Received: by 10.31.206.133 with HTTP; Thu, 9 Feb 2017 19:32:16 -0800 (PST) In-Reply-To: References: <1486683451.12698.83.camel@gerd-stolpmann.de> From:Yaron Minsky Date: Thu, 9 Feb 2017 22:32:16 -0500 Message-ID: To:Steffen Smolka Cc:Gerd Stolpmann , Jeremy Yallop , Caml List Content-Type: text/plain; charset=UTF-8 X-JS-Processed-by: mailcore Subject: Re: [Caml-list] namespace inside object? Sure. And if that's what you want, objects seem like the right solution. That said, I find this style hard to think about, since it involves breaking ordinary notions of abstraction. The more explicit style where you use functors (or functions, if using first class modules) to parameterize code feels cleaner and easier to reason about to, though it does have some more syntactic overhead. y On Thu, Feb 9, 2017 at 10:16 PM, Steffen Smolka wrote: > Right....but you have to explicitly make that choice when you implement `f`. > With objects on the other hand, somebody may write a base implementation c: > > class c = object(this) > method foo = ... this#bar ... > method bar = ... this#foo ... (* original definition *) > end > > Later on, somebody else may refine this implementation by overwriting c#bar: > > class c2 = object(this) > inherit c > method bar = ... (* new definition *) > end > > Now c2#foo will invoke the new definition of bar, even though the author of > `c` may have never anticipated this (and did not have to account for this > possibility when he defined foo). > > So, I would say "dynamic dispatch" plus inheritance give you the ability to > extend modules in ways that weren't necessarily anticipated by the original > author of the module. And yet all the code written by the original author > will work with your extended version of the module. > > -- Steffen > > > > On Thu, Feb 9, 2017 at 9:01 PM, Yaron Minsky wrote: >> >> I've always been a bit confused about what the term "dynamic dispatch" >> means, but don't first class modules provide what you want? After all, >> when you write: >> >> let f (module M : S) x = >> M.g x >> >> the specific function M.g is determined dynamically, depending on >> which first class module is passed into f. >> >> y >> >> On Thu, Feb 9, 2017 at 6:54 PM, Steffen Smolka >> wrote: >> > Yeah, I have seen that there is support for first class modules, that's >> > pretty cool stuff! >> > I do need dynamic dispatch, though. >> > >> > -- Steffen >> > >> > On Thu, Feb 9, 2017 at 6:37 PM, Gerd Stolpmann >> > wrote: >> >> >> >> Am Donnerstag, den 09.02.2017, 18:19 -0500 schrieb Steffen Smolka: >> >> >> >> Thanks for the detailed answer, Jeremy! >> >> >> >> If you're keen to stick with objects >> >> >> >> >> >> Yes, I rely on inheritance and dynamic dispatch for what I have in >> >> mind. >> >> (This is actually the first time I'm touching the dark object oriented >> >> side >> >> of OCaml :) ) >> >> >> >> To give some more context, I am refactoring some code that uses modules >> >> and no objects. The reason I want to move to objects is that I want to >> >> derive a slightly enhanced module from some base implementation. >> >> Inheritance >> >> + dynamic dispatch allow me to do so with very little trouble: I can >> >> simply >> >> overwrite a few methods from the base implementation. >> >> >> >> I suppose I could achieve the same by turning the base module into a >> >> functor, and abstracting over the functions that my enhanced >> >> implementation >> >> needs to replace. I think it won't be quite as natural, but I'll give >> >> that a >> >> try. >> >> >> >> >> >> First-class modules could also be an option: Let's assume both the >> >> base >> >> module and the modified one can use the same module type: >> >> >> >> module T = sig ... end >> >> >> >> Now, define the base module like >> >> >> >> module Base : T = >> >> ... >> >> end >> >> >> >> then, define the modified one: >> >> >> >> module Mod : T = >> >> include Base >> >> ... now override what you need to change but note that there's no >> >> dynamic dispatch ... >> >> end >> >> >> >> Of course, you could also use functors for making these modules. >> >> >> >> Now turn this into first-class modules and pass them around: >> >> >> >> let base = (module Base : T) >> >> let mod = (module Mod : T) >> >> >> >> The syntax for unpacking the module is quite cumbersome: >> >> >> >> let module M = (val base : T) in >> >> M.function ... >> >> >> >> Unfortunately, there's nothing simple like base.function. >> >> >> >> Compared with objects you get: >> >> >> >> You can also put types and (to some degree) modules into these "code >> >> containers" >> >> However, there's no dynamic dispatch except you arrange explicitly for >> >> that, e.g. with references to functions >> >> Generally, a heavier syntax, but it might be ok >> >> >> >> >> >> Gerd >> >> >> >> >> >> >> >> Or you could select the encoding using a variant type: >> >> >> >> >> >> Good idea, and I'm happy with the syntax for the caller. But I'm more >> >> concerned with the organization of the code; this would mix the Latin1 >> >> and >> >> Utf8 implementations. I would rather keep them separate. >> >> >> >> -- Steffen >> >> >> >> >> >> On Thu, Feb 9, 2017 at 5:55 PM, Jeremy Yallop wrote: >> >> >> >> Dear Steffen, >> >> >> >> On 9 February 2017 at 20:36, Steffen Smolka >> >> wrote: >> >> > Is it possible to create namespaces inside an object? Concretely, I >> >> > would >> >> > like to write >> >> > >> >> > class buffer = object(self) >> >> > ... >> >> > method get = ... >> >> > >> >> > module Latin1 = struct >> >> > method get = ... >> >> > end >> >> > >> >> > module Utf8 = struct >> >> > method get = ... >> >> > end >> >> > end >> >> > >> >> > so that given an object b : buffer, I can call methods >> >> > b#get >> >> > b#Latin1.get >> >> > b#Utf8.get >> >> >> >> It's possible to achieve something like this using methods that return >> >> objects. If your nested objects don't need to access the internal >> >> state of the parent then you might write it like this: >> >> >> >> class buffer = >> >> let latin1 = object >> >> method get = ... >> >> end >> >> and utf8 = object >> >> method get = ... >> >> end in >> >> object(self) >> >> ... >> >> method get = ... >> >> method latin1 = latin1 >> >> method utf8 = utf8 >> >> end >> >> >> >> With this approach you can write >> >> >> >> b#get >> >> b#latin1#get >> >> b#utf8#get >> >> >> >> which, apart from some minor orthographic differences, looks like what >> >> you were aiming for. >> >> >> >> Your intuition that this isn't really idiomatic OCaml is right, >> >> though. In OCaml, unlike some other languages with classes and >> >> objects, classes are not usually used as namespaces; method names are >> >> globally (or, rather, "ambiently") scoped, and there's no real support >> >> for the kind of nesting that you're interested in. Instead, people >> >> typically build nested namespaces using modules: >> >> >> >> module Buffer = >> >> struct >> >> let get = ... >> >> >> >> module Latin1 = struct >> >> let get = ... >> >> end >> >> >> >> module Utf8 = struct >> >> let get = ... >> >> end >> >> end >> >> >> >> With the module approach you write the 'receiver' after the 'method' >> >> rather than before, but that doesn't seem like a huge hardship. (10% >> >> of the world manages to get by with VSO languages.) >> >> >> >> Buffer.get b ... >> >> Buffer.Latin1.get b ... >> >> Buffer.Utf8.get b ... >> >> >> >> If you're keen to stick with objects there are slightly more idiomatic >> >> ways to make it work. You could, of course, replace the '.' with a >> >> '_' and define methods 'latin1_get', 'utf8_get' in place of >> >> 'Latin1.get', 'Utf8.get'. Or you could select the encoding using a >> >> variant type: >> >> >> >> type enc = Latin1 | Utf8 >> >> >> >> class buffer = >> >> object (self) >> >> method get = function >> >> | Latin1 -> ... >> >> | Utf8 -> ... >> >> end >> >> >> >> Of course, the order of the words in an invocation changes again, but >> >> there's no real increase in complexity for the caller: >> >> >> >> b#get Latin1 >> >> b#get Utf8 >> >> >> >> This last approach can be taken quite far -- for example, you could >> >> enrich the type 'enc' so that the return type of 'get' varies >> >> according to the encoding. >> >> >> >> Kind regards, >> >> >> >> Jeremy >> >> >> >> >> >> -- >> >> ------------------------------------------------------------ >> >> Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de >> >> My OCaml site: http://www.camlcity.org >> >> Contact details: http://www.camlcity.org/contact.html >> >> Company homepage: http://www.gerd-stolpmann.de >> >> ------------------------------------------------------------ >> >> >> > > >