From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id DAA30395; Tue, 20 Apr 2004 03:02:31 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id DAA30396 for ; Tue, 20 Apr 2004 03:02:29 +0200 (MET DST) Received: from us20.unix.fas.harvard.edu (us20.unix.fas.harvard.edu [140.247.35.200]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i3K13ajq014604 for ; Tue, 20 Apr 2004 03:03:37 +0200 Received: from [140.247.133.37] (roam133-37.student.harvard.edu [140.247.133.37]) by us20.unix.fas.harvard.edu (8.11.7p2/8.11.7) with ESMTP id i3K12SG07357 for ; Mon, 19 Apr 2004 21:02:28 -0400 Mime-Version: 1.0 (Apple Message framework v613) To: caml-list@inria.fr Message-Id: <836F0A79-9267-11D8-806B-0003939A19AA@fas.harvard.edu> Content-Type: multipart/alternative; boundary=Apple-Mail-1-1037014125 From: Mike Hamburg Subject: [Caml-list] O'Caml <=> Objective-C bridge Date: Mon, 19 Apr 2004 21:10:29 -0400 X-Mailer: Apple Mail (2.613) X-Miltered: at nez-perce by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; objective-c:01 command-line:01 objective-c:01 model:01 overflows:01 unacceptable:01 warrant:99 caml-:01 model:01 datatypes:01 struct:01 char:01 char:01 runtime:01 allocates:01 X-Attachments: type="text/enriched" Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk --Apple-Mail-1-1037014125 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed Greetings to the List! Having learned ML and several other functional languages in a programming class, I decided that it would be nice to be able to program in a functional language on my Mac. I chose caml for various reasons, partly because of the clean foreign-function interface. To make the language useful for more than command-line apps, I'm writing a bridge between caml and Objective-C (specifically, Cocoa). I have little experience in caml itself, and so have no intuition about idiomatic programming, especially with objects (most of my functional experience is in ML). What I want to know is, what would be a clean and idiomatic interface to this bridge on the caml side? Things that I've been considering in the design: -- Except by parsing the objective-c header files, you can't know what types a function takes and returns, and in particular it may return null. This is especially the case in objective-c because types and bindings are dynamic. As a result, you can't know much about a method when calling it from caml other than the number of arguments it takes, or even if the object answers it. (if it doesn't, this should throw an exception). -- You can send messages to nil in objc, and it will return nil. -- Objective-C uses 32-bit integers, but most methods that use them accept and return small numbers. The current model is to use ints, but throw an exception if it overflows... I'll probably add an int32 type to the interface as well for those times when this is unacceptable. -- Common things that would have to go across the interface quite often would be NSArray <=> list and NSString <=> string. These may be important enough to warrant special handling. -- Memory management can be messy. I've written the core of the bridge, at least in the caml->objc direction, and it works on a hello world program. The model is to look up classes and selectors (essentially methods) by name, and then to pass messages to a class or to an object. It consists of some datatypes and foreign functions in a module: > module ObjCGlue = struct > type objcid > type objcclass > type objcsel > > type cobj = NULL > | BOOL of bool > | INT of int > | CHAR of char > | STR of string > | FLOAT of float > | ID of objcid > | SEL of objcsel > | CLASS of objcclass > > (* starts up the runtime system, allocates an autoreleasepool *) > external _start : unit -> unit = "camlGlueStartup" > > (* returns a selector of the given name. remember those colons! *) > external _selNamed : string -> objcsel = "camlGlueStrToSel" > > (* returns a class of the given name, or null if none exists. > perhaps should throw an exception instead *) > external _classNamed : string -> cobj = "camlGlueStrToClass" > > (* sends a message to an object *) > external _message : objcid -> objcsel -> cobj list -> cobj > = "camlGlueMessageSend" > > (* invokes a class method *) > external _classMethod : objcclass -> objcsel -> cobj list -> cobj > = "camlGlueClassMethod" > > (* ... lots convenience functions to make testing easier ... *) > end;; Any suggestions would be much appreciated. Thanks a lot for your time, Mike Hamburg --Apple-Mail-1-1037014125 Content-Transfer-Encoding: 7bit Content-Type: text/enriched; charset=US-ASCII Greetings to the List! Having learned ML and several other functional languages in a programming class, I decided that it would be nice to be able to program in a functional language on my Mac. I chose caml for various reasons, partly because of the clean foreign-function interface. To make the language useful for more than command-line apps, I'm writing a bridge between caml and Objective-C (specifically, Cocoa). I have little experience in caml itself, and so have no intuition about idiomatic programming, especially with objects (most of my functional experience is in ML). What I want to know is, what would be a clean and idiomatic interface to this bridge on the caml side? Things that I've been considering in the design: -- Except by parsing the objective-c header files, you can't know what types a function takes and returns, and in particular it may return null. This is especially the case in objective-c because types and bindings are dynamic. As a result, you can't know much about a method when calling it from caml other than the number of arguments it takes, or even if the object answers it. (if it doesn't, this should throw an exception). -- You can send messages to nil in objc, and it will return nil. -- Objective-C uses 32-bit integers, but most methods that use them accept and return small numbers. The current model is to use ints, but throw an exception if it overflows... I'll probably add an int32 type to the interface as well for those times when this is unacceptable. -- Common things that would have to go across the interface quite often would be NSArray <<=> list and NSString <<=> string. These may be important enough to warrant special handling. -- Memory management can be messy. I've written the core of the bridge, at least in the caml->objc direction, and it works on a hello world program. The model is to look up classes and selectors (essentially methods) by name, and then to pass messages to a class or to an object. It consists of some datatypes and foreign functions in a module: module ObjCGlue = struct type objcid type objcclass type objcsel type cobj = NULL | BOOL of bool | INT of int | CHAR of char | STR of string | FLOAT of float | ID of objcid | SEL of objcsel | CLASS of objcclass (* starts up the runtime system, allocates an autoreleasepool *) external _start : unit -> unit = "camlGlueStartup" (* returns a selector of the given name. remember those colons! *) external _selNamed : string -> objcsel = "camlGlueStrToSel" (* returns a class of the given name, or null if none exists. perhaps should throw an exception instead *) external _classNamed : string -> cobj = "camlGlueStrToClass" (* sends a message to an object *) external _message : objcid -> objcsel -> cobj list -> cobj = "camlGlueMessageSend" (* invokes a class method *) external _classMethod : objcclass -> objcsel -> cobj list -> cobj = "camlGlueClassMethod" (* ... lots convenience functions to make testing easier ... *) end;;0000,6363,1212 Any suggestions would be much appreciated. Thanks a lot for your time, Mike Hamburg --Apple-Mail-1-1037014125-- ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners