Class Object Method Overloading

Propose new features and changes
RFM
Posts: 28
Joined: 21 May 2016, 12:50

Class Object Method Overloading

15 Feb 2017, 04:20

Hello,

Have been working on making a class and came across another very important thing I use very often in classes, but apparently cannot be done in AutoHotKey. That is method overloading. This is where you have two or more methods with the same name, just different parameters. For example, here are two methods with the same name, but different parameters, rather, one has an additional parameter (I omitted the entire class definition, as only method overloading is of concern):

Code: Select all

	GetDesc(Item, default := "")
	{
		; <some code>	
	}
	
	GetDesc(Item, default := "", ByRef found)
	{
		; <some code>	
	}
I use method overloading quite a bit, not to have it available here is very disappointing.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Class Object Method Overloading

15 Feb 2017, 06:48

Maybe you can work with variadic functions.
Also, note,
Optional parameters wrote: all parameters that lie to the right of the first optional parameter must also be marked optional. [v1.0.90+]:
source
Good luck.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Class Object Method Overloading

15 Feb 2017, 06:50

Code: Select all

MyFunction(Param1, Param2, Param3 := "") {
    If (Param3 = "")
        MsgBox, MyFunction() with 2 parameters
    Else
        MsgBox, MyFunction() with 3 parameters
}
:?:
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Class Object Method Overloading

15 Feb 2017, 17:46

Code: Select all

m:=new MyClass
m.GetDesc("a")
m.GetDesc("a","b")
m.GetDesc("c","d","e")
Class MyClass {
   GetDesc(v*){
      fun:=this["GetDesc" v.MaxIndex()]
      return %fun%(this,v*)
   }
   GetDesc1(Item){
      return this.GetDesc2(Item)
   }
   GetDesc2(Item, default := "")
   {
     MsgBox % A_ThisFunc "`n" Item "`n" default
   }

   GetDesc3(Item, default := "", ByRef found := "")
   {
     MsgBox % A_ThisFunc "`n" Item "`n" default "`n" found	
   }
}
4GForce
Posts: 553
Joined: 25 Jan 2017, 03:18
Contact:

Re: Class Object Method Overloading

15 Feb 2017, 20:37

HotKeyIt wrote:

Code: Select all

m:=new MyClass
m.GetDesc("a")
m.GetDesc("a","b")
m.GetDesc("c","d","e")
Class MyClass {
   GetDesc(v*){
      fun:=this["GetDesc" v.MaxIndex()]
      return %fun%(this,v*)
   }
   GetDesc1(Item){
      return this.GetDesc2(Item)
   }
   GetDesc2(Item, default := "")
   {
     MsgBox % A_ThisFunc "`n" Item "`n" default
   }

   GetDesc3(Item, default := "", ByRef found := "")
   {
     MsgBox % A_ThisFunc "`n" Item "`n" default "`n" found	
   }
}
That's nice to know, isn't there another way with __Call ???
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Class Object Method Overloading

16 Feb 2017, 02:51

Code: Select all

m:=new MyClass
m.GetDesc("a")
m.GetDesc("b","c")
m.GetDesc("d","e","f")
Class MyClass {
   __Call(func,v*){
      If !IsFunc(fun:=this[Func])
         fun:=this[Func v.MaxIndex()]
      return %fun%(this,v*)
   }
   GetDesc1(Item){
      return this.GetDesc2(Item)
   }
   GetDesc2(Item, default := "")
   {
     MsgBox % A_ThisFunc "`n" Item "`n" default
   }

   GetDesc3(Item, default := "", ByRef found := "")
   {
     MsgBox % A_ThisFunc "`n" Item "`n" default "`n" found	
   }
}

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 47 guests