in reply to C# reinvents @_

The idea of collecting all the unnamed arguments into a list harkens back to Lisp. It's a generally obvious way to handle them in untyped languages. Doing it in C++ would be a problem, because you need to know the type, and use some expression compiled to use that type (can't use a variable to hold the type itself!).

So if the C++ compiler gave you two arrays, one holding all the params as a list of void*'s, and the second holding typeid objects, the code can't do anything with the typeid other than compare for equality, and you can't do anything with the void* until it's cast back to the proper type. Now if you add the ability to cast to a typeid value instead of a literal type name, it might be useful.

Replies are listed 'Best First'.
Re: Re: C# reinvents @_
by hding (Chaplain) on Sep 19, 2001 at 19:35 UTC

    I had the same thought about Lisp when I first saw this post, but after looking around a bit, I wasn't able to find any way to do this properly in Common Lisp. (This isn't to say that there's not one; just that it's not immediately obvious to me.) With macros one can specify a &whole parameter to grab the argument list, but that's not valid for functions. And for functions one can start off the argument list with a &rest parameter which will grab the entire list of arguments, but then you can only use &key and &aux parameters afterward, no normal or &optional ones.

    Of course, this may be a limitation of Common Lisp only (or, as I said, maybe not; I may just be ignorant); I don't really know any other Lisps well enough to speculate.

      Says hding:
      I had the same thought about Lisp when I first saw this post, but after looking around a bit, I wasn't able to find any way to do this properly in Common Lisp.

      It seems to me that this is exactly what you want:

      (defun myfunction (&rest args) ...)

      In the body of myfunction, the parameter args is bound to the entire argument list, which is analogous to @_ in Perl.

      I don't understand your point about &optional. I thought we were looking for a way to simulate Perl's parameter-passing mechanism in Common Lisp; this is it. Perl doesn't have &optional; instead, you're supposed to examine the contents of @_ and make a decision depending on how many elements it contains. With the args parameter above, you are free to do the same thing in the same way.

      --
      Mark Dominus
      Perl Paraphernalia

        Yes, I mentioned this in another node somewhere in this discussion. I know that we can do that (which does suffice to address the original point). I was wondering about the extended question: is there a way to get the entire argument list while at the same time preserving the rest of Lisp's argument handling facilities? I may not have made that clear in this entry.

      Hmm, I thought you could have unnamed parameters collected into a list. My Lisp-dialects are 10 years rusty, and Common Lisp is just a manual to me. Maybe it was dropped from the standard, or has some more obsucre way to set it up. Or, maybe it's just so simple to throw another set of ()'s around the data that nobody cares.

      (foo one two (three four five))
      is similar to
      sub foo ($$$); foo ($one, $two, [$three, $four, $five]);
      with exactly three parameters according to the function, but the caller does the "collecting".

        I agree, it's the kind of thing that screams out "Lisp!", but I just couldn't think of how to do it. Guess we'll have to leave it to the Lisp historians.

        Update: Just in case anybody actually reads this and I'm not being clear, one can simulate the Perlish behavior simply by having a function like (defun fred (&rest args) (do-stuff-with args)). In fact, by treating args as a plist, one can simulate the Perl trick of passing a hash. What I can't see is how to get all the arguments as a list while still using Lisp's other facilities for handling them.