chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] MV in foreign-lambdas


From: tonyg
Subject: [Chicken-users] MV in foreign-lambdas
Date: Mon, 7 Oct 2002 21:06:52 +0100
User-agent: Mutt/1.2.5i

Just to clarify what I meant wrt wanting foreign-lambdas to return
multiple values...


What I'm after is a way of declaring that a
foreign-lambda/foreign-lambda* returns multiple values. For
foreign-lambda*, that's straightforward: instead of having a symbol
for a result type, you could put in an alist, just like for the
arguments:

(define sum-and-difference (foreign-lambda* ((int sum)
                                             (int diff))
                                            ((int op1)
                                             (int op2))
                                            "result.sum = op1+op2;"
                                            "result.diff = op1-op2;"))

and then:

(call-with-values
    (lambda () (sum-and-difference 3 2))
  (lambda (s d)
    (print "sum: " s)
    (print "diff: " d)))

You'd still have the old behaviour, too:

(define add1 (foreign-lambda* int ((int op))
                              "return(op + 1);"))

For foreign-lambda, it's a little trickier - since C procedures can't
return multiple-values :-) I want to define an extension to flag a
parameter as really being a result. Assuming the following C code
(it's very contrived):

int mul_sum_diff(int op1, int *sum, int *diff, int op2) {
  *sum = op1 + op2;
  *diff = op1 - op2;
  return op1 * op2;
}

...I was wanting to wrap that like this:

(define mul-sum-diff (foreign-lambda int
                                     "mul_sum_diff"
                                     int
                                     #:out int
                                     #:out int
                                     int))

which permits:

(call-with-values
    (lambda () (mul-sum-diff 3 2))
  (lambda (m s d)
    (print "mul: " m)
    (print "sum: " s)
    (print "diff: " d)))

Perhaps instead of saying "#:out <type>" you'd say "(<type> result)"
or something. I don't know which would be best.

Tony
-- 
Monkeys high on math -- some of the best comedy on earth
        - Tom Lord, regarding comp.lang.scheme




reply via email to

[Prev in Thread] Current Thread [Next in Thread]