Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 21

Thread: Varargs in ASH

  1. #1
    Developer Veracity's Avatar
    Join Date
    Mar 2006
    Location
    The Unseelie Court
    Posts
    12,920

    Default Varargs in ASH

    Is this cool, or what?

    Code:
    void test( int ... args )
    {
        print( "arg count = " + count( args ) );
        foreach index, arg in args {
    	print( "arg(" + index + ") = " + arg );
        }
    }
    
    // Test calling the vararg function with an array
    
    int [] zero = {};
    int [] one = { 1 };
    int [] two = { 1, 2 };
    int [] three = { 1, 2, 3 };
    
    test( zero );
    test( one );
    test( two );
    test( three );
    print( "----" );
    
    // Test calling the vararg function with multiple arguments
    
    test();
    test( 1 );
    test( 1, 2 );
    test( 1, 2, 3 );
    print( "----" );
    
    int min( int first, int ... args )
    {
        int retval = first;
        foreach index, arg in args {
    	if ( arg < retval ) {
    	    retval = arg;
    	}
        }
        return retval;
    }
    
    print( min( 100 ) );
    print( min( 100, 50 ) );
    print( min( 100, 50, 200, 25  ) );
    yields

    Code:
    > varargs.ash
    
    arg count = 0
    arg count = 1
    arg(0) = 1
    arg count = 2
    arg(0) = 1
    arg(1) = 2
    arg count = 3
    arg(0) = 1
    arg(1) = 2
    arg(2) = 3
    ----
    arg count = 0
    arg count = 1
    arg(0) = 1
    arg count = 2
    arg(0) = 1
    arg(1) = 2
    arg count = 3
    arg(0) = 1
    arg(1) = 2
    arg(2) = 3
    ----
    100
    50
    25
    It only works for user defined functions, so far. I may submit it so you-all can play with it, but I'd like it to work for built-in functions, too. I'd like a min() and max(), for example, that work with any number of args - just like I demonstarted with the ASH program.
    Ph'nglui mglw'nafh Cthulhu
    R'lyeh wgah-nagl fhtagn.

  2. #2
    Developer Veracity's Avatar
    Join Date
    Mar 2006
    Location
    The Unseelie Court
    Posts
    12,920

    Default

    Another example. It looks for exact type matches for parameters/types, and then non-exact - including varargs.
    Perhaps exact varargs should take precedence over functions that require coercion. Maybe not.
    Here's how it works now:

    Code:
    int mymax( int arg1, int arg2 )
    {
        print( "mymax(" + arg1 + "," + arg2 + ")" );
        return max( arg1, arg2 );
    }
    
    int mymax( int arg1, int arg2, int arg3 )
    {
        print( "mymax(" + arg1 + "," + arg2 + "," + arg3 + ")" );
        return mymax( arg1, mymax( arg2, arg3 ) );
    }
    
    int mymax( int arg1, int ... rest )
    {
        print( "mymax " + arg1 + " ... " + count( rest ) + " more args " );
        int retval = arg1;
        foreach i, arg in rest {
    	retval = mymax( retval, arg );
        }
        return retval;
    }
    
    print( mymax( 1, 2 ) );
    print( mymax( 1, 2, 3 ) );
    print( mymax( 1, 2, 10, 20, 4, 100, 12 ) );
    yields

    Code:
    > varargs2.ash
    
    mymax(1,2)
    2
    mymax(1,2,3)
    mymax(2,3)
    mymax(1,3)
    3
    mymax 1 ... 6 more args
    mymax(1,2)
    mymax(2,10)
    mymax(10,20)
    mymax(20,4)
    mymax(20,100)
    mymax(100,12)
    100

  3. #3
    Developer Veracity's Avatar
    Join Date
    Mar 2006
    Location
    The Unseelie Court
    Posts
    12,920

    Default

    I ran VMF (which does not use this, but uses almost everything else in the ASH language) on a character. It worked fine.
    I submitted this (experimental) feature in revision 19880.

    I'm leaving this open for comment and suggestions and bug reports.

  4. #4
    Senior Member AlbinoRhino's Avatar
    Join Date
    May 2008
    Posts
    1,040

    Default

    This is pretty interesting. Does it only apply to arrays/multiples of ints? (I haven't updated yet or I would just try it with some strings.)

  5. #5
    Developer Veracity's Avatar
    Join Date
    Mar 2006
    Location
    The Unseelie Court
    Posts
    12,920

    Default

    I used int, because it was simple, but you can use literally any type there. int, string, maps, records, whatever.

  6. #6
    Senior Member AlbinoRhino's Avatar
    Join Date
    May 2008
    Posts
    1,040

    Default

    Even sweeter. I am thinking most people would expect an exact type match function to take precedence over a more loose match? I could be wrong.

  7. #7
    Senior Member zarqon's Avatar
    Join Date
    Nov 2007
    Location
    Seoul, Korea
    Posts
    3,750

    Default

    This is exciting! Thanks Veracity. I'll have to update ZLib's min() and max() to accept unlimited arguments now.

    I suppose something like

    Code:
    int myfunction(int ... goodints, int ... badints) {
    should be illegal because there's no way of knowing when the good ints break bad. But consecutive varargs of different types should be fine? Just thinking of possible ways to break it while on my lunch break.

    Perhaps exact varargs should take precedence over functions that require coercion. Maybe not.
    Originally Posted by Veracity View Post
    I would expect that and be mildly surprised if it were otherwise, probably. Easy enough to work around, though.
    Sig by JakAtk
    My scripts: Prefref Plus | Skillref Plus | One-Click Wossna | Om*****st (??) | Psychose-a-Matic | RandBot
    Combat suite: Best Between Battle | Mercenary Mood | SmartStasis | BatMan | BatMan RE
    For script authors: ASH Wiki | ZLib | BatBrain | CLI Links | Drag-n-Drop Inventories | CanAdv | Script Registry | Map Manager
    If you appreciate my work, help me become BAT KING OF THE WORLD! Thanks to all donators!

  8. #8
    Developer Veracity's Avatar
    Join Date
    Mar 2006
    Location
    The Unseelie Court
    Posts
    12,920

    Default

    I keep it simple. You get a single vararg argument, which must be the last.

    In Java, your vararg can get an array of generic Objects. You get one, at the end, because it takes all the remaining arguments and you can figure them out on your own.

    In ASH, they have to be the same type, since we don't have generic Objects (or null). I suppose I could have multiple vararg arguments, grouping together different types, but that seems complicated to code and to understand. SO, I think not.

  9. #9
    Senior Member zarqon's Avatar
    Join Date
    Nov 2007
    Location
    Seoul, Korea
    Posts
    3,750

    Default

    Yeah one per function at the end makes sense and seems likely to cover most uses.
    Sig by JakAtk
    My scripts: Prefref Plus | Skillref Plus | One-Click Wossna | Om*****st (??) | Psychose-a-Matic | RandBot
    Combat suite: Best Between Battle | Mercenary Mood | SmartStasis | BatMan | BatMan RE
    For script authors: ASH Wiki | ZLib | BatBrain | CLI Links | Drag-n-Drop Inventories | CanAdv | Script Registry | Map Manager
    If you appreciate my work, help me become BAT KING OF THE WORLD! Thanks to all donators!

  10. #10
    Senior Member
    Join Date
    Apr 2009
    Posts
    1,871

    Default

    Unless varargs cannot be empty, you can't even do multiple vargargs of the same type even with another type in between. Not to mention, what if the you do two types where one can be coerced to another, or pass arguments that can be coerced to one or another ... doing anything else than other languages do would be a nightmare.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •