What is the TRIPLE_DOT character and what causes errors regarding it?

I have had a few and heard of many errors and mentions to the TRIPLE_DOT character but none of them seem to explain what it means and how it interacts with code. I would greatly appreciate it if someone told me how the TRIPLE_DOT character interacts with code and what it is used for. I thought at first it was used in making default/optional arguments for functions but I think I am incorrect there... <== (hey I used a triple dot I think i get it now loljk) It would be nice to know how to make functions that if one argument is null it assigns a default value to it. I could have used that a lot to simplify my code... <==(there it is again)

Thank's in advance!

Answers

  • The question isn't clear...

    But basically, the triple dot is used when a function accepts a variable number of arguments. The parameter is actually an array of arguments.

    void setup()
    {
      foo("a", "b");
      foo("x", "y", "z");
    }
    
    void foo(String... arguments)
    {
      for (String a : arguments)
      {
        println(a);
      }
    }
    

    Sorry, but Java has no concept of default value. You have to do the test for null and assign the default value yourself.

  • Sorry if it wasn't clear. That is sort of what i mean by optional arguments like how you can have a PVector class and use either xy or xyz coordinates in all of the functions. Can you show me more on how to use the TRIPLE_DOT character? I have almost no idea on how to use it. I can begin to see how you use String... arguments and such but getting the args in the function is kind of blurry.

  • edited November 2013

    ...how you can have a PVector class and use either xy or xyz coordinates in all of the functions.

    We don't need "varargs" for that! We just need to create methods/constructors using same identifier, but w/ diff. signature:

    class PVector {
    
      float x, y, z;
    
      PVector(float xx, float yy) {
        this(xx, yy, 0);
      }
    
      PVector(float xx, float yy, float zz) {
        x = xx;
        y = yy;
        z = zz;
      }
    }
    
  • edited November 2013

    And I believe latest print() & println() from Processing v2.1+ is using ... for arguments! <:-P

  • Wait, you can create tow different constructors with two different sets of arguments and instructions for the same class? MIND = BLOWN

  • Although Java doesn't have default parameter values; creating methods/constructors w/ diff. signatures are as close as we can get! :>

  • edited November 2013

    But another question? Is it even fathomable to do this with multiple types of arguments. All the compiler would have to do is the same thing with varargs but this time sort through the types and just say "that's a string put it in that array" and then say "thats an int put it in there" and run code like normal. Right?

  • Take a look at this post to check a custom Colour class I've made which abuses diff. signatures: ;))

    http://forum.processing.org/two/discussion/1220/best-way-to-sort-through-a-6000-element-array-based-on-distance#Item_9

  • Some nice code there!

  • edited November 2013

    When we got methods/constructors w/ same identifier, Java has to determine which 1 to use! =:)
    Data-types & # of arguments determine the signature of a method/constructor! :-B

  • Cool, so can you have a function like this:

    foo(String... stringList, int... intList)
    {
       //DO SOME STUFF WITH THE VARARGS
    }
    
  • edited November 2013

    According to the explanation link, if ... is used, it must be last argument. And we can't have more than 1 either!

  • Oh okay, well at least we can use it with other args so long as they precede the vararg. that would have been cool though for a lot of things i have done. My physics engine i'm currently making uses well, a lot of stuff like this and this will remove a lot of lines from the code. SOSOSO sorry that im keeping you here with question after question but i have one more. It might sound stupid but is there a way to have functions with different signatures? I have tried before but I might have missed something.

  • edited November 2013

    Methods & constructors are functions! Function is just a generic name, while former 1s are specific! o->
    In my Colour class you're gonna find both of them! :D
    Perhaps I should try to re-write Colour class constructors and its set() methods using "varargs" :-\"

  • Thank you so much I learned a ton about programming today! I tested it out with the following code and it worked!

    void setup()
    {
      noLoop();
    }
    void draw()
    {
      foo(1,2,3);
      foo("Hello", "World!");
    }
    void foo(int a, int b, int c)
    {
      println(a+b*c);
    }
    void foo(String a, String b)
    {
      println(a + " " + b);
    }
    
  • edited November 2013

    Finally modified Colour to use "varargs" for both constructor & set() method: \m/

    /** 
     * Colour Container III (v3.11)
     * by GoToLoop (2013/Sep)
     * 
     * forum.processing.org/two/discussion/1273/
     * what-is-the-triple_dot-character-
     * and-what-causes-errors-regarding-it
     */
    
    import java.util.Arrays;
    
    final String[] myColors = {
      "Tomato", "Gold", "Green", "Saddle Brown", 
      "Teal", "Khaki", "Tan", "Olive"
    };
    
    Colour[] palette;
    
    final static String NAME = "RGB.txt", SEPARATOR = ",";
    
    void setup() {
      size(600, 400);
      frameRate(.5);
    
      palette = loadPalette(NAME, SEPARATOR);
    
      println(palette);
      println();
    }
    
    void draw() {
      final int idx = frameCount % myColors.length;
      final String name = myColors[idx];
    
      final int search = Arrays.binarySearch(palette, name);
    
      final Colour found = palette[search];
      final color c = found.get();
    
      background(c);
    
      println("#" + nf(search, 2) + "  " + found);
    
      frame.setTitle("Index: " + idx + "\tColor: " 
        + found.name + "\tCode: " + hex(c, 6));
    }
    
    Colour[] loadPalette(String path, String delim) {
      final String[] lines = loadStrings(path);
      final int len = lines.length;
    
      final Colour[] RGBs = new Colour[len];
    
      for (int i = 0; i != len;) {
        final String[] units = split(lines[i], delim);
        final String name = units[0];
        final color[] channels = int( trim(subset(units, 1)) );
    
        RGBs[i++] = new Colour(name, channels);
      }
    
      Arrays.sort(RGBs);
      return RGBs;
    }
    
    class Colour implements Comparable {
      short R, G, B;
      String name;
    
      Colour(String label, color... c) {
        name = label;
        set(c);
      }
    
      void set(color... c) {
        final int len = c.length;
    
        if (len == 1) {
          if (c[0] >= 0 & c[0] < 0400)  setGrey(c[0]);
          else                          setColor(c[0]);
    
          return;
        }
    
        if (len > 0)  R = (short) constrain(c[0], 0, 0xFF);
        if (len > 1)  G = (short) constrain(c[1], 0, 0xFF);
        if (len > 2)  B = (short) constrain(c[2], 0, 0xFF);
      }
    
      void clear() {
        R = G = B = 0;
      }
    
      void setColor(color c) {
        R = (short) (c >> 020 & 0xFF);
        G = (short) (c >> 010 & 0xFF);
        B = (short) (c & 0xFF);
      }
    
      void setGrey(color c) {
        R = G = B = (short) constrain(c, 0, 0xFF);
      }
    
      color get() {
        return color(R, G, B);
      }
    
      color[] toArray() {
        return new color[] {
          R, G, B
        };
      }
    
      String toString() {
        return "\"" + name + "\"  \t [ " +
          R + ", " + G + ", " + B + " ]" +
          "\t [ " + hex(get(), 6) + " ]";
      }
    
      int compareTo(Object other) {
        return other instanceof Colour ? 
        name.compareTo( ( (Colour) other ).name ) : 
        name.compareTo( (String) other );
      }
    }
    }
    
  • "RGB.txt":

    Black, 0
    Blue,0,0, 255
    Cyan, 0, 255, 255
    Gray ,      128
    Tan, 210, 180, 140
    Green, 0, 128, 0,,,
    Magenta, 255, 0, 255
    Red, 255, 0, 0
    White, -1
    Yellow, 255, 255, 0
    Saddle Brown, 140, 70, 20
    Orange, 255, 165, 0
    Tomato, 255, 100, 70
    Pink, 255, 192, 203
    Gold, 255, 215, 0
    Khaki, 240, 230, 140
    Indigo, 75, 0, 130
    Dark Slate Blue, 70, 60, 140
    Olive, 128, 128, 0
    Teal, 0, 128, 128
    Dim Gray, 105
    
  • Nice! If only i had the ability to write code like that so well. To be completely honest I'm kind of a noob when it comes to programming.

  • this, having same method name with different argument types / numbers, is called 'polymorphism' btw

  • Yes, particularly, function overloading, which is a subtype of Polymorphism.

    And, println_hello_world, you probably will be able to write such code after some years of experience... :-)

  • edited November 2013

    Indeed, there are overloading & overriding polymorphism types. :-c

  • For the sake of completeness I have modified @PhiLho's example to show that you don't have to pass any values.

    void setup()
    {
      foo("a", "b");
      foo("x", "y", "z");
      foo();
    }
    
    void foo(String... arguments)
    {
      println(arguments.length); // zero if no parameters
      for (String a : arguments)
      {
        println(a);
      }
    }
    
  • Thank you all for the amazing help and knowledge. This forum almost has more info than the javadoc and processing website does!

Sign In or Register to comment.