[plt-scheme] Style question
Tomasz wrote:
> Hello.
>
> I'm writing simple PostgreSQL connection pool (spgsql based). I've
> little scheme experience and I have style question. I've defined pool
> as structure (basically opaque type from user perspective):
>
> (define-struct pool (max-size queue connection-producer connection-
> closer custodian))
>
> and i've written constructor-like function (provided from module):
>
> (define (create-pool max-size connection-producer connection-closer)
> (let ((pool (make-pool max-size (make-queue max-size) connection-
> producer connection-closer (make-custodian))))
> ;; some more initialization snipped for clarity
> pool))
>
> I was wondering if this is "proper" style. How is constructor-like-
> function idiom typically implemented in Scheme? At first it seemed to
> me that "make-pool" would be more appropriate (maybe with some magic
> prop:constructor function property to initialize) but on the other
> hand I'm not entirely sure that such overloading of standard make-
> <type> meaning is apropriate...
I think exporting the function above as 'make-pool' is entirely
reasonable, even though it isn't a "pure constructor". If you look in
the reference, there are a lot of 'make-' procedures that do something
other than just create a struct instance containing the procedure's
arguments.
How to do it? Define it as above and export it using 'rename-out':
(provide pool?
(rename-out [create-pool make-pool]))
There are other small tricks you can play to get the procedure to print
with the external name instead of the internal name. Something like the
following:
(define create-pool (let ([make-pool (lambda ___)]) make-pool)
or just use 'procedure-rename'.
Ryan