[SUGGEST][AHKv2.0-a108] types

Discuss the future of the AutoHotkey language
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

[SUGGEST][AHKv2.0-a108] types

15 Jan 2020, 05:31

In AHKv2.0-a108 I found 3 types of functions:
  • "Func"
  • "BoundFunc"
  • "Closure" => I suggest "Closure" to be changed to "ClosureFunc".
In this case ALL 3 types will contained "Func" and will be separated from non functions.
Will be cool if "is" to work like:

Code: Select all

if value is "Func" ;return TRUE if type of value contain "Func"
if(inStr(type(value), "Func")) ;like this one
In numbers it is TRUE but using additional word "number":
  • Integer => IntegerNumb
  • Float => FloatNumb

Code: Select all

if value is "number" ;return TRUE if type of value is Integer or Float
if value is "Numb" ;return TRUE if type of value contain "Numb"
AHKv2.0 alpha forever.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [SUGGEST][AHKv2.0-a108] types

15 Jan 2020, 06:25

Code: Select all

class FuncModel {

}
would break your idea.
Recommends AHK Studio
User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: [SUGGEST][AHKv2.0-a108] types

15 Jan 2020, 20:21

Really we just need a simple and concise way to determine if what is held in a variable is callable.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [SUGGEST][AHKv2.0-a108] types

15 Jan 2020, 20:37

Code: Select all

variable.HasMethod('Call')
User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: [SUGGEST][AHKv2.0-a108] types

15 Jan 2020, 20:40

swagfag wrote:
15 Jan 2020, 20:37

Code: Select all

variable.HasMethod('Call')
👍🏻
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

Re: [SUGGEST][AHKv2.0-a108] types

16 Jan 2020, 05:00

nnnik wrote:
15 Jan 2020, 06:25

Code: Select all

class FuncModel {

}
would break your idea.
nnnik is right.
If type string contain some 'Type' it is not the solution.
swagfag wrote:

Code: Select all

variable.HasMethod('Call')
Very elegant solution from swagfag but actually not the complete solution.

Code: Select all

class ClassFunction {
	Call() {
		MsgBox('ClassFunction')
}	}
class holder {
	__New(acti) {
		MsgBox('argument.HasMethod("Call") = ' acti.HasMethod('Call'))
		this.acti:= (acti.HasMethod('Call')? acti: (*)=>MsgBox('default')).Bind(this)
}	}
h:= holder.new(ClassFunction.new()) 
/* ---------------------------
[a108] TEST.ahk
---------------------------
Error:  This value of type "ClassFunction" has no method named "Bind".

	Line#
	011: {
	000: }
	012: {
	013: MsgBox(acti.HasMethod('Call'))
	014: {
	014: Return MsgBox('default')
	014: }
--->	014: this.acti:= (acti.HasMethod('Call')? acti: (*)=>MsgBox('default')).Bind(this)
	016: }
	018: h := holder.new(Func('Function'))
	019: h.acti.call()
	020: h := holder.new(ClassFunction.new())
	021: h.acti.call()
	023: {
	024: While (this.__Class != 'Any')

The current thread will exit.
---------------------------
OK   
---------------------------*/
h.acti.call()
I made some tests and the right question is: Is there in inheritance chain some BASE that is 'Func' (in this case).
And I make a small function that return TRUE if there so BASE and FALSE else.

Code: Select all

isType(this, type) {
	while(this.__Class != 'Any')
		if(this.__Class == type)
			 return 1
		else this:= this.base
	return 0
}
As everything in AHKv2.0 is some kind of OBJECT, all 'things' have property '__Class' that contain name of given OBJECT.
For example:

Code: Select all

1		= Integer.Integer.Number.Primitive.Any
1.1		= Float.Float.Number.Primitive.Any
""		= String.String.Primitive.Any
()=>	= Func.Func.Object.Any
Now what the function isType(this, type) can do?

Code: Select all

isType(1, 'Integer') ; TRUE
isType(1, 'Number') ; TRUE
isType(1.1, 'Integer') ; FALSE
isType(1.1, 'Number') ; TRUE
And lets return to example.

Code: Select all

class ClassFunction {
	Call() {
		MsgBox('ClassFunction')
}	}

class holder {
	__New(acti) {
		MsgBox('argument.HasMethod("Call") = ' acti.HasMethod('Call'))
		this.acti:= (isType(acti, 'Func')? acti: (*)=>MsgBox('default')).Bind(this)
}	}

h:= holder.new((this)=>MsgBox('Noname function >> ' this.__Class))	;set acti as function
h.acti.call()
h:= holder.new(ClassFunction.new()) ;set acti as default function
h.acti.call()
Now we exactly know is that value has base 'Func'.

EDIT: An example that can return fake positive result.

Code: Select all

class Func {
	Call() {
		MsgBox('ClassFunction')
}	} ;In this case Func.Func.Object.Any
I think in the future reserved words cannot be used as variable or class names.
AHKv2.0 alpha forever.
User avatar
lvalkov
Posts: 58
Joined: 08 Mar 2019, 16:39

Re: [SUGGEST][AHKv2.0-a108] types

19 Jan 2020, 22:18

kczx3 wrote:
15 Jan 2020, 20:21
Really we just need a simple and concise way to determine if what is held in a variable is callable.
swagfag wrote:
15 Jan 2020, 20:37

Code: Select all

variable.HasMethod('Call')
_3D_ wrote:
16 Jan 2020, 05:00
Very elegant solution from swagfag but actually not the complete solution.
I fail to see how the provided solution is incomplete. In order to deem an object "callable", the only constraint that has to be satisfied is that the object (or any of its prototypes) implement a Call() method. For querying that, HasMethod('Call') suffices fully.

Code: Select all

; Error:  This value of type "ClassFunction" has no method named "Bind".
; --->	014: this.acti:= (acti.HasMethod('Call')? acti: (*)=>MsgBox('default')).Bind(this)
You attempt to invoke Bind() on said function object. It predictably fails, throwing an exception as no implementation for Bind() has been provided. You seem to equate "callable" with "necessarily a derivative of Func" (hence, supporting Bind() and any of Func's other methods and properties out of the box). You are, however, wrong to assume that.
_3D_ wrote:
16 Jan 2020, 05:00
the right question is: Is there in inheritance chain some BASE that is 'Func'
This question is also wrong. The true right question is: How do I force my custom function object to behave identically to an instance of Func?
Unfortunately, the answer to that question is "you can't". Not yet at least and not without providing your own implementation. I have just finished a write-up on how one could go about doing that.

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 28 guests