From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 4D8C6BCAE for ; Tue, 28 Jun 2005 01:21:53 +0200 (CEST) Received: from mail.gmx.net (pop.gmx.net [213.165.64.20]) by concorde.inria.fr (8.13.0/8.13.0) with SMTP id j5RNLqkg001487 for ; Tue, 28 Jun 2005 01:21:53 +0200 Received: (qmail invoked by alias); 27 Jun 2005 23:21:52 -0000 Received: from p54A3369E.dip0.t-ipconnect.de (EHLO [192.168.2.136]) [84.163.54.158] by mail.gmx.net (mp013) with SMTP; 28 Jun 2005 01:21:52 +0200 X-Authenticated: #20477425 From: Michael Wohlwend To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] state pattern... Date: Tue, 28 Jun 2005 01:23:54 +0200 User-Agent: KMail/1.8 References: <200506262157.02201.micha-1@fantasymail.de> <3fce1487ee8fd9f28edbe70276980346@wetware.com> In-Reply-To: <3fce1487ee8fd9f28edbe70276980346@wetware.com> X-Face: S)[vu%Bha1d&ej9GfwAq~7C}A,y[B.uS}+D6'hb~xPwsxymw$fnCOaMe<=?utf-8?q?*bnUajSBR=5Fm=3FR=0A=09?=@V3;iX8[A}z`.%pEQ1r7iZhN8#ktTCBQ}&mkx>=RH&l|l6\]NZI@ MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200506280123.55251.micha-1@fantasymail.de> X-Y-GMX-Trusted: 0 X-Miltered: at concorde with ID 42C08A10.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 woodyatt:01 'self:01 'self:01 val:01 val:01 endline:01 endline:01 ...:98 ...:98 wrote:01 simpler:01 imperative:01 tend:02 parameter:02 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: On Monday 27 June 2005 00:13, james woodyatt wrote: > I tend to work in the functional style first, then transfer what I've > done into imperative style if there is a performance gain to be had by > it. Here's what I would do to make something like what you want: > ... I have modified the code, so that some method(s) of the state classes get the context class as parameter; I think it took me 3 hour to get the type definitions working :-) Can this be done simpler? It works, although I don't completly understand all of it :-) ------------------------- class type param = object method name : string end class type state = object('self) method show: 'a. (#param as 'a) -> unit method next: 'self end class ['state] context s = object (this:'self) val name = "context" val state_: #state = s method name = name method show: unit = state_#show this method run = {< state_ = state_#next >} end class state1 = object (this) method show: 'a. (#param as 'a) -> unit = fun c -> print_endline ("state1 of " ^ c#name) method next = new state2 end and state2 = object (this) method show: 'a. (#param as 'a) -> unit = fun c -> print_endline ("state2 of " ^ c#name) method next = new state1 end;; let c = new context (new state1) in c#show; let c = c#run in c#show ---------------------------- thanks, Michael