color and int do not provide a different signature for overloaded constructors

_vk_vk
edited May 2016 in Programming Questions

in a class I try to provide two different contructors:

Obj ( PVector pos, color c){
//stuff
}

Obj ( PVector pos, int i){
//stuff
}

but as color is an int this do not work... Is there a way to make this happens?

thanks

Answers

  • edited June 2015 Answer ✓

    color doesn't exist in Java. Consequently it needs to be transpiled as int before compilation.
    Maybe use float in place of int? :-/
    Take notice that doing so it's gonna be incompatible w/ "JS Mode".
    Since for the latter all numbers are double.

  • Thanks, that is what I have figured, just want to be sure, thanks. i gotta be an int. I'll have to make less options.

  • edited June 2015 Answer ✓

    P.S.: Another solution would be to swap their places:

    method (PVector pos, color c) {
      //stuff
    }
     
    method (int i, PVector pos) {
      //stuff
    }
    
  • _vk_vk
    edited June 2015

    Humm that worked, but maybe is a little confusing. thanks again. I thought the order won't matter. The real constructor takes a lot more than this two...

  • edited June 2015

    As far as I know color exists in Processing to make things easier but it behaves like an int in most cases. I say behaves like because in every context I've seen it is interchangeable with int

    I'm pretty sure you could get away with just one function. If you prefer color then use that, personally I use int instead of color in my sketches:

    void setup() {
      size(100, 100);
      int a = 1;
      color b = 2;
      println(foo(a)); // 1
      println(foo(b)); // 2
      println(bar(a)); // 1
      println(bar(b)); // 2
      exit();
    }
    
    color foo(color c) {
      return c;
    }
    
    int bar(int i) {
      return i;
    }
    
Sign In or Register to comment.