[Wish] Wish to add support for custom objects to allow calling a method directly

Discuss the future of the AutoHotkey language
QiuDao
Posts: 18
Joined: 25 Mar 2021, 22:55

[Wish] Wish to add support for custom objects to allow calling a method directly

29 Apr 2021, 09:41

@lexikos
Dear Lexikos, from my experience in code writing, it will truly lose much convenience if we can not call a method from a custom object directly followed by a dot.
With this support, we could write:

Code: Select all

class Cls1{
	aa:='a1'
	f(){
		OutputDebug(this.aa)
		,OutputDebug(this.bb)
	}
}
ob:=Cls1()
f(){
	{base:ob,bb:'b2'}.f()
	,{base:ob,bb:'c3'}.f()
}
f()
However, without this support, we have to write:

Code: Select all

; AHK VSERION: AutoHotkey_2.0-a133-9819cb2d
class Cls1{
	aa:='a1'
	f(){
		OutputDebug(this.aa)
		,OutputDebug(this.bb)
	}
}
ob:=Cls1()
f(){
	_:=0 ; it's usually unnecessary.
	,{base:ob,bb:'b2'}.f()
	,{base:ob,bb:'c3'}.f()
}
Is there any necessity to add a line like _:=0 or a:={base:ob,bb:'b2'},a.f() in some situations just to avoid an error to make the code run? By the way, in my opinion, due to the brace } is followed by a dot, It would never conflict with any previous syntax.
Compared with defining a property directly to an object like ob.bb:='b2', instead, by creating a custom object based on ob which contains the property bb for extending, the original object ob shared by derived objects could avoid being modified. It would greatly improve flexibility and convenience in some situations.
To sum up, I really wish to add support to allow a custom object to call a method in one line as an expression which it always be.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [Wish] Wish to add support for custom objects to allow calling a method directly

29 Apr 2021, 10:12

QiuDao wrote:
29 Apr 2021, 09:41
Is there any necessity to add a line like _:=0 or a:={base:ob,bb:'b2'},a.f() in some situations
there is not. ({base:ob,bb:'b2'}).f() works in all situations, and since ure throwing objects away, as would (ob.f)({bb:'b2'})
QiuDao
Posts: 18
Joined: 25 Mar 2021, 22:55

Re: [Wish] Wish to add support for custom objects to allow calling a method directly

29 Apr 2021, 10:29

swagfag wrote:
29 Apr 2021, 10:12
QiuDao wrote:
29 Apr 2021, 09:41
Is there any necessity to add a line like _:=0 or a:={base:ob,bb:'b2'},a.f() in some situations
there is not. ({base:ob,bb:'b2'}).f() works in all situations, and since ure throwing objects away, as would (ob.f)({bb:'b2'})
Thanks for reply. It does work wrapped by ()! But (ob.f)({bb:'b2'}) is not equal, because the object does not contain property aa.
And, I think, it may still seem a little redundant to be wrapped with (), due to using {} without () will never conflict.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: [Wish] Wish to add support for custom objects to allow calling a method directly

29 Apr 2021, 21:37

{ is block-begin. Beginning a line with an object literal is rare. Allowing both (without parentheses) based on what's between or after the braces would be unnecessary complication, and would potentially introduce ambiguity.

There is no problem with directly invoking an object literal. The issue is only whether you have an object literal in the first place, or a block or syntax error.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [Wish] Wish to add support for custom objects to allow calling a method directly

29 Apr 2021, 22:05

QiuDao wrote:
29 Apr 2021, 10:29
But (ob.f)({bb:'b2'}) is not equal, because the object does not contain property aa
true. regardless
QiuDao
Posts: 18
Joined: 25 Mar 2021, 22:55

Re: [Wish] Wish to add support for custom objects to allow calling a method directly

30 Apr 2021, 02:17

lexikos wrote:
29 Apr 2021, 21:37
{ is block-begin. Beginning a line with an object literal is rare. Allowing both (without parentheses) based on what's between or after the braces would be unnecessary complication, and would potentially introduce ambiguity.

There is no problem with directly invoking an object literal. The issue is only whether you have an object literal in the first place, or a block or syntax error.
@lexikos
Thank you for your reply. Fortunately, the problem can be solved by being wrapped with (). It is not that inconvenient for me.
By the way, I am used to using object literal at the beginning of the line very frequently, instead of calling an object method with parameters. With this infinite-base style, it can achieve calling a method without any parameters. Also, I think, it can be considered as a habitual practice in OOP and get enhanced.

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: OpalMonkey and 100 guests