Re: [Vala] Ellipsis syntax (Was: Ellipsis as an argument..)



On Tue, 2009-02-24 at 22:43 +0100, Didier "Ptitjes" wrote: 
Hello Shawn!

Hello.. pleased to meet you!


public static void foo(params string[] args) {
      // ...
}

So if your arguments can be subclasses of GObject, you could use "params 
Object[] args".

You know.. that's what I thought and when I tried it, it didn't work for
me: (vala 0.5.6)

public class SMF {

  public void test ( params string[] args ) {
    stdout.printf("%s\n",args[1]);
  }

  public static void main() {
    var x = new SMF ();
    x.test("a","b","c","d");
  }
}

$ valac x.vala -o x
x.c: In function ‘smf_main’:
x.c:26: warning: passing argument 2 of ‘smf_test’ from incompatible
pointer type
x.c:26: warning: passing argument 3 of ‘smf_test’ makes integer from
pointer without a cast
x.c:26: error: too many arguments to function ‘smf_test’
error: cc exited with status 256
Compilation failed: 1 error(s), 0 warning(s)

I then assumed it must be reserved for passing in parameters for the
entry point of a program, because this works:

public class SMF {

  public static void main( params string[] args ) {
    stdout.printf("%s\n",args[1]);
  }
}

Perhaps the fact that this doesn't work, is a bug? As far as the types
of arguments, my needs are simple. alpha/numeric. Nothin' fancy. GValue,
I suppose, to have a variety of alpha/numeric. (Hrmm.. can string and
int autocast to GValue? I'll have to look that one up.. I digress)

In the end, I decided that a single array object was a better approach
for me anyway. (I'd still love to see something like this work though)

I would be glad to implement it and propose a patch if something gets 
consensus. Any ideas ?

I think the params approach is fine. Provided that's what it was
intended to address. (Seems like it is in C#.. not sure why it'd be
different in Vala) Perhaps your patch can address that?

Unfortunately, though, I would only consider myself well versed in Perl
and Shell, so if there is a better way, I'm not sure I would know it.
(I've always wanted to learn C.. but lacked a mentor to barrage with
questions.. vala seems easier, so hopefully I'll pick it up faster)

Regards,
Shawn




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]