Method overloading

  1. last year

    Marc

    20 Jun 2019 User since 2018
    Edited last year by Marc

    I'm using method overloading quite a lot in other non-Creo projects. It would be nice to have this in Gravity at some point.

    For now I can use work-arounds, so this is not a showstopper for me.

    With normal methods I could just use methods with different names. A bit messy, but still a decent work-around.

  2. marco

    20 Jun 2019 Administrator User since 2016

    Hi @Marc you mean method overloading for Creo native controls and classes?

  3. Marc

    20 Jun 2019 User since 2018

    @marco Hi @Marc you mean method overloading for Creo native controls and classes?

    No, for my own classes. For example:

    class Person {
    	var _age = 0
    	var _name = "John Doe"
    	
    	func SetDefaults(name) {
    		_name = name	
    	}
    	
    	func SetDefaults(age, name) {
    		_age = age	
    		_name = name	
    	}
    	
    }
  4. marco

    20 Jun 2019 Administrator User since 2016

    What about:

    class Person {
    	var _age = 0
    	var _name = "John Doe"
    	
    	func SetDefaults(name , age) {
    		 if (name) _name = name
    		if (age) _age = age	
    	}
    }

    Then you can call:

    var p = Person();
    p.SetDefaults("John");
    p.SetDefaults("John", 30);
  5. Marc

    20 Jun 2019 User since 2018

    @marco What about:

    class Person {
    	var _age = 0
    	var _name = "John Doe"
    	
    	func SetDefaults(name , age) {
    		 if (name) _name = name
    		if (age) _age = age	
    	}
    }

    Then you can call:
    var p = Person(); p.SetDefaults("John"); p.SetDefaults("John", 30);

    That is a reasonable approach as long as Creo will keep accepting this when you're further down the road with implementing strong typing.

    In a language as for example C#, which is strongly typed, you're not allowed to leave out defined parameters, unless they're decorated with the "Optional" tag.

    If Creo will continue allowing this, then your example will be fine for me.

  6. marco

    20 Jun 2019 Administrator User since 2016

    If you want to be safe for future language improvements then you can write:

    class Person {
    	var _age
    	var _name 
    	
    	func SetDefaults(name = "John Doe" , age = 0) {
    		 if (name) _name = name
    		if (age) _age = age	
    	}
    }

    please note that instead of SetDefaults you could have an init function (the constructor) so your code will be much more easier to read. There is a small bug in the init constructor (when called with a different number of arguments) so you should wait few days for the next upcoming release.

  7. Marc

    20 Jun 2019 User since 2018
    Edited last year by Marc

    @marco If you want to be safe for future language improvements then you can write:

    Ah ok, just like optionals in C#.

    @marco please note that instead of SetDefaults you could have an init function

    Yes I know. This was just an example.

    @marco There is a small bug in the init constructor (when called with a different number of arguments)

    I know, I did experience that bug today already ... ;)

or Sign Up to reply!