caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Zheng Li <zheng_li@users.sourceforge.net>
To: caml-list@inria.fr
Cc: warren@metaweb.com
Subject: Re: killing a cat
Date: Tue, 09 Sep 2008 16:11:17 +0200	[thread overview]
Message-ID: <48C68405.2020900@users.sourceforge.net> (raw)
In-Reply-To: <0233247D-28D8-4C83-9EFB-6FD4B50B3D8B@metaweb.com>

Hi,

Warren Harris wrote:
> Now, I could get around this by making "f" be a function that when 
> evaluated returns the result of the helper. However, I'm trying to 
> avoid that because in the real implementation, the work of the helper
>  is somewhat expensive and should only be performed once. An
> alternate solution can be achieved by pushing the universal qualifier
> for the result type down into the handler:
> 
> type 'a handler = { handle : 'b . 'a -> (string -> 'a -> 'b, 'b) 
> writer }
I think it's reasonable to use universal qualifier here.

> I am forced into reworking the helper parameter function, wf, to also
>  become a record in order for it to be universally qualified:
> 
> type 'a field_handler = { write_field : 'b . string -> 'a -> (unit ->
>  string -> 'a -> 'b) -> 'b }
> 
> This works:
> 
> let helper name wf = { handle = fun a k -> wf.write_field name a k }
>  let f = helper "f" write_int_field

It's hard to tell where a better option is without knowing your overall
design on types and data structures. On the other hand, I would usually
restrict universal qualifiers into a range as small as possible. In your
example, I would probably define as the follows:

<code>
type 'a handler = { handle: 'a -> (string * 'a) writer }
and  'a writer = { write: 'b. (unit -> 'a -> 'b) -> 'b }

let helper name wf = { handle = wf name }
let write_int_field s i = { write = fun k -> k () (s, i+1) }
let f = helper "f" write_int_field
</code>

which seems simpler to me. However this really depends on your overall
design.

HTH.

--
Zheng


      reply	other threads:[~2008-09-09 14:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-09-08 18:37 Warren Harris
2008-09-09 14:11 ` Zheng Li [this message]

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

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

  git send-email \
    --in-reply-to=48C68405.2020900@users.sourceforge.net \
    --to=zheng_li@users.sourceforge.net \
    --cc=caml-list@inria.fr \
    --cc=warren@metaweb.com \
    /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 NNTP newsgroup(s).