From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 3E9EBBCAE for ; Sun, 26 Jun 2005 21:56:24 +0200 (CEST) Received: from mail.gmx.net (pop.gmx.net [213.165.64.20]) by nez-perce.inria.fr (8.13.0/8.13.0) with SMTP id j5QJuNXO018643 for ; Sun, 26 Jun 2005 21:56:23 +0200 Received: (qmail invoked by alias); 26 Jun 2005 19:56:23 -0000 Received: from p54A32FCA.dip0.t-ipconnect.de (EHLO [192.168.2.136]) [84.163.47.202] by mail.gmx.net (mp020) with SMTP; 26 Jun 2005 21:56:23 +0200 X-Authenticated: #20477425 From: Michael Wohlwend To: caml-list@yquem.inria.fr Subject: state pattern... Date: Sun, 26 Jun 2005 21:57:02 +0200 User-Agent: KMail/1.8 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="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200506262157.02201.micha-1@fantasymail.de> X-Y-GMX-Trusted: 0 X-Miltered: at nez-perce with ID 42BF0867.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; val:01 mutable:01 endline:01 endline:01 cheers:01 ...:98 inherit:01 inherit:01 constraint:01 implemented:02 unit:03 unit:03 pattern:03 let:03 context:04 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: Hi, although it can be implemented with a 'match', just of interest, can somebody help me with my oo implementation of the state-pattern? this does not work (any many other tries also didn't :-) class ['a] context (start_state : 'a)= object(this) constraint 'a = #state val mutable state = start_state method set_state st = state <- st method show = state#show method run = state#handle this end and virtual state = object method virtual show : unit method virtual handle : state #context -> #state -> unit end;; class state1 = object (this) inherit state method show = print_endline "state1" method handle context = context#set_state (new state2) end and state2 = object (this) inherit state method show = print_endline "state2" method handle context = context#set_state (new state1) end;; for this to work: let c = new context (new state1) in c#show; (* state1 *) c#run; (* change state *) c#show; (* state2 *) c#run; cheers, Michael