Re: optional arguments in CLush functions?
Leon Bottou <[email protected]>
| Newsgroups | gmane.lisp.lush.devel |
|---|---|
| Message-ID | <[email protected]> |
On Tuesday 15 November 2005 07:56 pm, Ralf Juengling wrote: > Hi, > > When I write C functions where the arguments are objects > (pointers) and want to make some of the arguments optional, > an easy way to do so is by the convention that NULL pointers > indicate absent arguments. > > I'd like to do something similar with CLush. For the sake > of an example, say I might want to write my own vector copying > routine and if three args are provided, then the last arg is > taken to be the storage for the result. > > Of course, it doesn't work currently--at least not the > way I tried. There are a number of questions here: No the compiler does not support this. > 1. Is there any way, currently, to do what I want to do? Use the 'overload' facility... Exemple: (libload "overload") [overload.lsh] ? (de f1(a) ((-flt-) a) (* a a ))) = "/home/leonb/lush/lsh/libstd/overload.lsh" = f1 ? (de f2(a b) ((-flt-) a b) (+ (* a a)(* b b)))) = f2 ? (dhc-make () f1 f2) Preprocessing and parsing f1 ... Preprocessing and parsing f2 ... Generating C for f1 ... Generating C for f2 ... gcc -DHAVE_CONFIG_H -DNO_DEBUG -Wall -O3 -march=i686 -mmmx -msse -pthread ..... ? (overload f f1 f2) = ::DM:883fcf4 ? (f 2) = 4 ? (f 3 4) = 25 Note that <f> compiles, but is slow in interpreted mode because it uses lisp code to match its arguments and their types. When <f> is compiled, the overload resolution happens in the compiler... > 2. When you try to create a NULL ptr (a gptr object with > value NULL) in lush, the result is nil. When the CLush > compiler encounters a nil, it considers it a boolean > value rather than a NULL pointer. Well, if you declare that the argument is a gptr, it will take it as a null pointer. The problem is that you cannot declare that a variable can be either a null gptr, or another object. This is a limitation of the type system. The overload facility suggests how we could handle this at compile time, but the type system in the compiler does not support this. > 3. Would it be possible to change the CLush compiler to > make it accept a NULL ptr as arguments where the > corresponding formal parameter is an object? Not without major redesign. - L. ------------------------------------------------------- This SF.Net email is sponsored by the JBoss Inc. Get Certified Today Register for a JBoss Training Course. Free Certification Exam for All Training Attendees Through End of 2005. For more info visit: http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click