Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

C# reinvents @_

by stefp (Vicar)
on Sep 19, 2001 at 03:43 UTC ( [id://113241]=perlmeditation: print w/replies, xml ) Need Help??

It is no better to be the man of one language than to be the man of one book. So I am studying C# and find it quite to my taste. No small feat after too much Perl programming: C feels too low level. C++ is too complicated. Java is just bondage.

The problem with a new language is: one wants his favorite idioms to translate readily. So what about @_ in C#?

I love @_ (and I hate it, but I know how to use a hash ref if I want to have some equivalent of named parameters). The idea is that the caller passes as many parameters as it wants. The caller gets them in @_ and figures out what to do with them. You knew that. :)

So I was curious to see how this idiom of using an implicit array translates in C#.

// defining public int foo ( params Object[] vals ) { foreach( Object i in vals ) { ... } } // calling foo( 1, 4);
the params keyword indicates that an unknown nunber of parameters can be used for the calling of the function foo and that they will be available in the callee in the vals array.

In C you would have to use va_start(),va_arg(), va_end(). Neither Java, nor C++ have foreach keyword, so they must use an explicit iterator object. I am too lazy to write the equivalent Java code. Too ugly.

you may want to read A comparative overview of C#.

-- stefp A unix fan that likes a MS product. Everything happens!!

Replies are listed 'Best First'.
Re: C# reinvents @_
by John M. Dlugosz (Monsignor) on Sep 19, 2001 at 10:22 UTC
    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.

      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

        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".

Re: C# reinvents @_
by princepawn (Parson) on Sep 19, 2001 at 03:53 UTC
    I have been looking at Gwydion Dylan and it is a very interesting language.

    You might also read a paper on how the multiple dispatch of dylan allowed for much easier code conversion than was possible with Java.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://113241]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found