[AHKv2.0-a108] Object members exception

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

[AHKv2.0-a108] Object members exception

08 Jan 2020, 06:54

And again about __Get and Has but this time for classes.
In AutoHotkey, variables are created simply by using them.

Code: Select all

;In AutoHotkey, variables are created simply by using them.
class Item {
	__New() {
		;this.stat:= "" ;uncomment to init first
		this.stat:=!this.stat	;variable (object member) is NOT created simply by using it
								;Object.__Get() return exception but must return ""
}	}

stat:=!stat		;variable is created simply by using it
o:= Item.new()	;exception
Who needs this?
Who needs (in script language) define -> init -> use conception ???
Variables this way BUT object members other.

Why all the time needs to check Has this or that and else init it.
In other hand Object.SomeMember:= "" is valid.

I don`t understand why.
Last edited by _3D_ on 15 Jan 2020, 06:09, edited 4 times in total.
AHKv2.0 alpha forever.
User avatar
fincs
Posts: 527
Joined: 30 Sep 2013, 14:17
Location: Seville, Spain
Contact:

Re: [AHKv2.0-a108] Object members exception

08 Jan 2020, 07:05

In neither case is the target of the operation an already-initialized variable/field:
this.stat:=!this.stat: This is a GET operation on a not-yet-existing field followed by a SET. Since it's reading from a field that doesn't exist it throws an exception.
stat:=!stat: This reads an uninitialized global variable, and therefore it triggers a warning when #Warn (UseUnsetGlobal) is used.
A common recommendation even in scripting languages is to initialize variables you're going to use later with an actual predictable value.
fincs
Windows 11 Pro (Version 22H2) | AMD Ryzen 7 3700X with 32 GB of RAM | AutoHotkey v2.0.0 + v1.1.36.02
Get SciTE4AutoHotkey v3.1.0 - [My project list]
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

Re: [AHKv2.0-a108] Object members exception

08 Jan 2020, 07:34

fincs wrote:
08 Jan 2020, 07:05
In neither case is the target of the operation an already-initialized variable/field:
this.stat:=!this.stat: This is a GET operation on a not-yet-existing field followed by a SET. Since it's reading from a field that doesn't exist it throws an exception.
stat:=!stat: This reads an uninitialized global variable, and therefore it triggers a warning when #Warn (UseUnsetGlobal) is used.
A common recommendation even in scripting languages is to initialize variables you're going to use later with an actual predictable value.
I understand why this happens ;Object.__Item.Get() return exception but must return "", but don`t understand who is needed ???
Let see next:

Code: Select all

class Item {
	__New() {
		this.stat:= "" 		  ;create member
		this.stat:=!this.stat ;this.stat become TRUE
}	}

o:= Item.new() ;here we corectly create and init this.stat
o.DeleteProp("stat")
MsgBox(o.stat) ;exception the same but more complicated
So if user need to f.cked up script he can.
And the same in AHKv2.0-a100

Code: Select all

;AHKv2.0-a100
class Item {
	__New() {
		this.stat:=!this.stat ;this.stat become TRUE
}	}
o:= new Item()
MsgBox(o.stat)
So JUST if Object.__Item.Get() return "" all of this equilibristics become unnecessary.
I override Array and Map How I think Array and Map must work in AHKv2.0 (current version a108) , now I will override Object too, BUT it is unnecessary at all for AHK.
AHKv2.0 alpha forever.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [AHKv2.0-a108] Object members exception

08 Jan 2020, 07:54

the mechanics have been reversed.
previously, returning blank was the default and if u wanted protection against accidental reads of nonexistent properties u would have had to implement something custom in metaget urself.
now its the other way around. presumably, noobs will be less likely to become victims of ahk pitfalls theyre not yet aware of, and those who do need the old default mechanic back will know enough to reimplement it themselves.

my protip: just declare ur instance fields in the class body and stop making new fields inside constructors and methods. saves wasting time digging through the entire class to figure out what fields are present and where they came from
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

Re: [AHKv2.0-a108] Object members exception

08 Jan 2020, 10:26

swagfag wrote:
08 Jan 2020, 07:54
the mechanics have been reversed.
previously, returning blank was the default and if u wanted protection against accidental reads of nonexistent properties u would have had to implement something custom in metaget urself.
now its the other way around. presumably, noobs will be less likely to become victims of ahk pitfalls theyre not yet aware of, and those who do need the old default mechanic back will know enough to reimplement it themselves.

my protip: just declare ur instance fields in the class body and stop making new fields inside constructors and methods. saves wasting time digging through the entire class to figure out what fields are present and where they came from
You are completely right.
If I need to do pitfalls in my code language give me freedom to do them.

Code: Select all

;Override Object --------------------------------------------------------------
;Override Object.__Get --------------------------------------------------------
Object.Prototype.DefineMethod('__Get' , func("Object__Get" ))
Object__Get(this, name, params*) {
	if(this.HasOwnProp(name))
		 return this.name[params*]
	else return ''	
} 
;Override Object.__Call -------------------------------------------------------
Object.Prototype.DefineMethod('__Call', func("Object__Call"))
Object__Call(this, name, params*) {
	if(this.HasOwnMethod(name))
		 return this.name(params*)
	else return ''	
} ;----------------------------------------------------------------------------

class myClass { ;without strange init in constructor
	meth() => this.stat:=!this.stat ;__Get '' and __Set this.stat:=!''
	prop(arg) => arg
}

o:= myClass.new()
MsgBox("__Get stat -> "  o.stat)		;__Get  unknown  prop
MsgBox("__Call meth -> " o.meth())		;__Call existing method
MsgBox("__Get stat -> "  o.stat)		;__Get  existing prop
MsgBox("__Get xxxx -> "  o.xxxx)		;__Get  unknown  prop
MsgBox("__Call prop -> " o.prop("123"))	;__Call existing method
MsgBox("__Call yyyy -> " o.yyyy("123"))	;__Call unknown  method
And again You are completely right.
There too many things that is complicated to debug.
AHKv2.0 alpha forever.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [AHKv2.0-a108] Object members exception

09 Jan 2020, 06:14

Please ask questions like these in Ask for Help.
Recommends AHK Studio
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

Re: [AHKv2.0-a108] Object members exception

10 Jan 2020, 01:21

nnnik wrote:
09 Jan 2020, 06:14
Please ask questions like these in Ask for Help.
nnnik, I don`t ask for help, I am disagree with object concept. So place of this post must be in "AutoHotkey v2 Development".
AHKv2.0 alpha forever.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [AHKv2.0-a108] Object members exception

10 Jan 2020, 03:13

Sorry I must have misread the topic then - as far as I understood you were asking for help on how to cope with the new object changes.
Recommends AHK Studio

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: lexikos, ThePeter and 31 guests