From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Panicz Maciej Godek Newsgroups: gmane.lisp.guile.user Subject: Re: dynamic-wind Date: Fri, 30 Jun 2017 23:48:23 +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 1498859337 19529 195.159.176.226 (30 Jun 2017 21:48:57 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 30 Jun 2017 21:48:57 +0000 (UTC) Cc: "guile-user@gnu.org" To: Catonano Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Fri Jun 30 23:48:53 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 1dR3mV-0004ng-Lw for guile-user@m.gmane.org; Fri, 30 Jun 2017 23:48:51 +0200 Original-Received: from localhost ([::1]:46091 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dR3ma-0000gM-TH for guile-user@m.gmane.org; Fri, 30 Jun 2017 17:48:56 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58745) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dR3m8-0000YP-Az for guile-user@gnu.org; Fri, 30 Jun 2017 17:48:29 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dR3m7-00051K-7J for guile-user@gnu.org; Fri, 30 Jun 2017 17:48:28 -0400 Original-Received: from mail-wm0-x234.google.com ([2a00:1450:400c:c09::234]:38352) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dR3m6-0004z2-UH for guile-user@gnu.org; Fri, 30 Jun 2017 17:48:27 -0400 Original-Received: by mail-wm0-x234.google.com with SMTP id f67so4494981wmh.1 for ; Fri, 30 Jun 2017 14:48:25 -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=YfZp2l8YAgouxOpuLgDwpefVkw/57BLppalAyT83KjU=; b=NoLc5jrudHLxxxLXdQfgvmhj6VGZQWLy4fRuFdsU/RUeDKvb8GwwlhRJ8Hpj7EQKuX JN7etdrRE9sTw1Bz6DSKh4dDNpXlkhixs4vkIkQ9UyJOIWpX9/PwQVQU6ujNqXoONzDn 9YHndLf9A996avMYEDKXiECjr1PJsMWTBFagWYc9DfKiHXQfr2QXU72JlywA5U2UWYnd ZPk6fx3eJ8S6rvnRXYc75/x3/mUnX4E8EsSBkxXTgyjfb8KuRoKCEfgazhCgTHcyllmn hEVs8F0klyAJlQV4xUuDG2O5tQQ7eiHSgS0BifpUtw2oJ55svToRd5WB2lDIjVVNZCwE 9v8w== 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=YfZp2l8YAgouxOpuLgDwpefVkw/57BLppalAyT83KjU=; b=poU+tsDZc6gSoim23r5/TyyRnONn8NpX4agM1NNne/O7iuXv1uHQ/CVSt8gPOXMYoD Vi61Pp7kRyqMdil7VtSSt2YXygzUUrCenDqtDa+4O8vKLUxDprxaA04PSfpYIpvIk3V6 b1cnHThAWGUPdsSKR2lWul3yvRTbVx/oWjBQUXtZgFrvMH2KIH4/AfjKwZxRnsZQcTXc q6EhKYnRvyiIlRX46WuxyHjJw4NsUrpVKfyHzX3v2YSWn5tOZ7ocmuyZgoVa88aNqSex ZD4jH1e68qGDJszE2jx0n02v58XVaN8AMWGLuJJxLVycDu3547cAD07Lo5rBSvw9CmJD moIw== X-Gm-Message-State: AKS2vOw5tsmZVww28hZqU4c+x0NIG7R3CprtQBwL+eYi6uqlvQmeehxD KZuSCCgAlolIWoPRYQGfe8q3iLgfYQ== X-Received: by 10.80.155.19 with SMTP id o19mr6669264edi.27.1498859304178; Fri, 30 Jun 2017 14:48:24 -0700 (PDT) Original-Received: by 10.80.183.174 with HTTP; Fri, 30 Jun 2017 14:48:23 -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::234 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:13891 Archived-At: 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