unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Catonano <catonano@gmail.com>
To: Panicz Maciej Godek <godek.maciek@gmail.com>
Cc: "guile-user@gnu.org" <guile-user@gnu.org>
Subject: Re: dynamic-wind
Date: Sun, 2 Jul 2017 08:00:58 +0200	[thread overview]
Message-ID: <CAJ98PDxbY3654q0mEJBFVHMyQCsFwPvHP6J6KH5poS6k8jHJuQ@mail.gmail.com> (raw)
In-Reply-To: <CAMFYt2YEtzUWkqRveeJRR8GXwQ_Dom6t_saLYy8_=XsfOgy3QQ@mail.gmail.com>

2017-06-30 23:48 GMT+02:00 Panicz Maciej Godek <godek.maciek@gmail.com>:

>
>
> 2017-06-30 22:33 GMT+02:00 Catonano <catonano@gmail.com>:
>
>> 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)


  reply	other threads:[~2017-07-02  6:00 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-30 20:33 dynamic-wind Catonano
2017-06-30 21:48 ` dynamic-wind Panicz Maciej Godek
2017-07-02  6:00   ` Catonano [this message]
2017-07-02  6:01     ` dynamic-wind Catonano
2017-07-02 11:58     ` dynamic-wind Chris Vine
2017-07-05  6:14       ` dynamic-wind Catonano
2017-07-05  8:23         ` dynamic-wind David Kastrup
2017-07-08 20:03         ` dynamic-wind Amirouche Boubekki
2017-07-08 21:34           ` dynamic-wind Marko Rauhamaa
2017-07-09  7:21             ` dynamic-wind David Kastrup
  -- strict thread matches above, loose matches on Subject: below --
2017-07-09 12:59 dynamic-wind Chris Vine
2017-07-09 14:09 ` dynamic-wind Vítor De Araújo
2017-07-09 14:49   ` dynamic-wind Chris Vine
2017-07-17 10:04     ` dynamic-wind Catonano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAJ98PDxbY3654q0mEJBFVHMyQCsFwPvHP6J6KH5poS6k8jHJuQ@mail.gmail.com \
    --to=catonano@gmail.com \
    --cc=godek.maciek@gmail.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).