Nov 23 2004

PLT v299 is too sweet

Published by matt at 08:29 under

In a file called lib.c:

int addTwo(int x, int y) {
  return x + y;
}

Compiled on the Powerbook with:
gcc -dynamiclib -o lib.dylib lib.c

Then, in DrScheme v299:

(require (lib "foreign.ss"))
(define addTwo
  (get-ffi-obj
   "addTwo" "lib"
   (_fun _int _int -> _int)))

And, in the interactions window, I can now use:

> (addTwo 2 2)
4

That is so very sweet. Dynamic FFI. Thank you, Eli.

PS. The cool bit is the definition of ‘addTwo’ in Scheme; it’s a function name, a library name, and a type contract. How sweet is that?!? This makes it so much easier to solve a problem I’ve been having with native number types from PLT Scheme. So much goodness, it hurts.

2 Responses to “PLT v299 is too sweet”

  1. Jonon 26 Nov 2004 at 13:53

    Shouldnt that be (addTwo 5 3) ? ;)

  2. Matton 26 Nov 2004 at 15:34

    Absolutely correct. And the result should be ‘8′.

    I’m ashamed for having presented an example that doesn’t result in the number 8. Alas, I set a poor example.