From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Catonano Newsgroups: gmane.lisp.guile.user Subject: Re: dynamic-wind Date: Sun, 2 Jul 2017 08:00:58 +0200 Message-ID: References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" X-Trace: blaine.gmane.org 1498975292 5580 195.159.176.226 (2 Jul 2017 06:01:32 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 2 Jul 2017 06:01:32 +0000 (UTC) Cc: "guile-user@gnu.org" To: Panicz Maciej Godek Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sun Jul 02 08:01:26 2017 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dRXwi-000160-LT for guile-user@m.gmane.org; Sun, 02 Jul 2017 08:01:24 +0200 Original-Received: from localhost ([::1]:56872 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dRXwn-0000Vu-Vx for guile-user@m.gmane.org; Sun, 02 Jul 2017 02:01:30 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59025) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dRXwN-0000Uu-VR for guile-user@gnu.org; Sun, 02 Jul 2017 02:01:05 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dRXwM-0003r3-6M for guile-user@gnu.org; Sun, 02 Jul 2017 02:01:04 -0400 Original-Received: from mail-wm0-x230.google.com ([2a00:1450:400c:c09::230]:37080) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dRXwL-0003ol-RR for guile-user@gnu.org; Sun, 02 Jul 2017 02:01:02 -0400 Original-Received: by mail-wm0-x230.google.com with SMTP id i127so79675939wma.0 for ; Sat, 01 Jul 2017 23:00:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=NIP120BPXMmDqbBOBUzbQUkRqMm9GttNuz7gKE9rEC0=; b=sZHiTdEYsLAooH1N53t/2eaf5lO9/FpRvExOfpOYNYVxAN5gv3iFKOgDlhror2xBnG qkr++GYFrI4Ym0yeTCO+Flx2SW5tqtCFYXs7CNM/xLyxPuvs/rntot6p5nGoXfKeEkdq F+rn1/EiTPWgpf/Uz0/A6TEJTrUgD0kJXOQ3rZv0h4MZbz0i7sCfUukt85kl9viu6F8x jJFWmDXfsZ5aUeSbzCs3BQH1zuF+V11b2eCnEXyC2rcC0h6EzwpA1vzy0A2YyrAoNe4A PTmrQm0dAdBtFaSratmfRaZpr85zrkvDqBpMOPt/xRlAFmRMZMvrUXCWg5MLQO1EpRDL 0otA== 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=NIP120BPXMmDqbBOBUzbQUkRqMm9GttNuz7gKE9rEC0=; b=mbOk9K87AE1HQ3nrZCrdYRsUJ4eQ1cBVufU9XipJRl4NEVoRfC0oUsQC7GRpwQX6hP CYNAOSbTqg85pVKjpQgXF2o6hfv2Drl3mt/vRU5NcxExW+tqCwmINPlbrnUTP9aRwjQu wcznfQHKOubq9AbA/awG1kRNJVrIbgaDMLS97rr1UWQBbe1s409JQmR4zokFaNMxGh7W m/9/+8BIIJqgyjpRWH/u5Br6eEuUW6HY7QT4yRqqmbcA13dynrWwT7r53Kr4M3owGreb XQmyaWFGsE49N3BpioiR3G4DBUcpM7mLR2RtNK6A824Hd/T5EB8CXVaauz3uSeefvCll OZBg== X-Gm-Message-State: AKS2vOylmhr8p4YWA8+b/CmbF4oGM+YYEj0EJ+ymIqEKrssUG9EYj+/f 9RFPRqF985rcRVwnVd2ELtPAYbZ9X0EbVdM= X-Received: by 10.80.149.133 with SMTP id w5mr10720393eda.82.1498975258695; Sat, 01 Jul 2017 23:00:58 -0700 (PDT) Original-Received: by 10.80.131.4 with HTTP; Sat, 1 Jul 2017 23:00:58 -0700 (PDT) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::230 X-Content-Filtered-By: Mailman/MimeDel 2.1.21 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:13892 Archived-At: 2017-06-30 23:48 GMT+02:00 Panicz Maciej Godek : > > > 2017-06-30 22:33 GMT+02:00 Catonano : > >> On the irc channel I was suggested that it might have been a good fit for >> my use case >> >> I took a look at it in the manual >> >> I'm perplexed. I don't understand it >> >> How is it supposed to be used ? >> >> > It's very simple (at least from the point of view of a user) > When it is tempting to write something like > > (define (within-context action) > (enter-context) > (action) > (leave-context)) > > you simply change it to > > (define (within-context action) > (dynamic-wind > (lambda () (enter-context)) > action > (lambda () (leave-context)))) > > The thing is, that in general (action) may transfer control outside of the > scope of that particular context (like, using call/cc or exceptions) -- and > in such situations, we would like the (leave-context) handler to be > invoked. If and we ever get back there, we wish that the (enter-context) > were invoked again. > > > >> The provided example is somewhat contrived, I couldn't understand it >> anyway. >> >> My use case is basic, really. >> >> I have a scheme wrap around a C library for reading xls files, freexl. >> >> Freexl uses a pointer to a structure tha represents the opened xls file >> and >> its contents >> >> Each function writes/reads in the memory region pointed to such pointer. >> >> In the end, it requires to use a function that closes the file AND frees >> all the involved structures in memory. >> >> So my idea was that I would have gotten a simple macro, like this >> >> (with-xls-file "path/to/my/xls-file.xls" handler-ptr >> (do-something handler-ptr) >> (do-something-more handler-ptr)) >> >> and this would have expanded to >> >> (freexl-open "path/to/my/xls-file.xls" handler-ptr) >> (freexl-do-something handler-ptr) >> (freexl-do-something-more handler-ptr)) >> (freexl-close handler-ptr) >> >> Do I need dynamic-wind at all ? >> > > If you don't use dynamic-wind, some of the possible use cases will not be > covered. Non-local transfers of control will break the system. > > I believe you'd like to assume that there shouldn't be any non-local > transfers of control, but actually you can't know this. And the interface > to dynamic-wind is very straightforward, so there's no excuse for not using > it. > > Here's a simple real life example in Scheme: > > (define current-working-directory getcwd)(define change-directory chdir) > > (define (with-changed-working-directory dir thunk) (let ((cwd (current-working-directory))) (dynamic-wind (lambda () (change-directory dir)) thunk (lambda () (change-directory cwd))))) > > HTH > > aahh I see now Well. the manual is not clear enough, I think, in this regard In fact, I think Amirouche got tricked too When I was playing with his library for accessing a Wiredtiger based db, there was a bug that when some query went wrong, the connection to the db didn t get released and so the subsequent connections couldn t be established I had to quit Guile and start it again. I was using (with-context some-connection (query...)) The code for with-context is here (and it doesn t use dynamic-wind as far as I can see) https://framagit.org/a-guile-mind/guile-wiredtiger/blob/master/wiredtiger/extra.scm#L328 I think that the use case should be explicitly stated, in the manual, as you did here Also the example should be about some db or network connection, not about call/cc So that understanding dynamic-wind and its use case wouldn t require understanding call/cc (which I still don t get)