objects: properties (static/dynamic)

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

objects: properties (static/dynamic)

Post by jeeswg » 06 Sep 2019, 22:46

I have some queries re. properties in AHK v2:
- Is it possible to change the default getter/setter for an object's key-like properties? (The ones that don't have descriptors.)
- What is the best way to tell if a property is dynamic/doesn't have descriptors? [I give a way below.]
- If we are calling properties with descriptors, 'dynamic', then what do we call key-like properties (that don't have descriptors)? The opposite of dynamic is static, but 'static' has a specific meaning re. class/instance properties.

Some example code for properties:

Code: Select all

;non-existent properties v. keys:
oMap := Map()
;MsgBox(oMap["key"]) ;Error:  Key not found.
MsgBox(oMap.prop) ;(blank)

;assigning properties v. keys:
oMap := Map()
MsgBox(ObjOwnPropCount(oMap) " " oMap.Count) ;0 0
Loop 65535
	vChar := Chr(A_Index)
	, oMap.%vChar% := 1 ;property
	, oMap[vChar] := 1 ;key
MsgBox(ObjOwnPropCount(oMap) " " oMap.Count) ;65509 65535

;call method dynamically without %%:
oMap := Map(StrSplit("k1,v1,k2,v2,k3,v3,k4,v4,k5,v5", ",")*)
MsgBox(oMap.Count) ;5
vMtd := "Delete"
oMap.GetMethod(vMtd).Call(oMap, "k3")
MsgBox(oMap.Count) ;4

;call property dynamically without %%:
oMap := Map(StrSplit("k1,v1,k2,v2,k3,v3,k4,v4,k5,v5", ",")*)
vProp := "Count"
vCount := Map.Prototype.GetOwnPropDesc(vProp).Get.Call(oMap)
MsgBox(vCount) ;5

;get/set property value dynamically without %%:
oMap := Map(StrSplit("k1,v1,k2,v2,k3,v3,k4,v4,k5,v5", ",")*)
vProp := "k6"
Map.Prototype.GetOwnPropDesc("__Item").Set.Call(oMap, "v6", vProp)
MsgBox(oMap.Count) ;6
vProp := "k3"
vValue := Map.Prototype.GetOwnPropDesc("__Item").Get.Call(oMap, vProp)
MsgBox(vValue) ;v3

;get property info (non-existent/simple key-like/dynamic):
oMap := Map()
;prop0 is undefined
oMap.prop1 := "value1"
oMap.DefineProp("prop2", Object())
for _, vProp in ["prop0", "prop1", "prop2"]
{
	if oMap.HasOwnProp(vProp)
	{
		try
			ObjOwnPropCount(oMap.GetOwnPropDesc(vProp))
			, vType := "D"
		catch
			vType := "S"
	}
	else
		vType := "_"
	vOutput .= vType " " vProp "`r`n"
}
MsgBox(vOutput)
SOME AHK V1/V2 DIFFERENCES RE. KEY/PROPERTIES

The changes are subtle, yet significant, highlighted by the use of asterisks 8 times below.

AHK v1 basic objects:
case-insensitive keys
case-insensitive properties (with getters/setters)
sharing the same namespace
non-existent keys/properties return a blank string

AHK v2 maps:
case-*sensitive* keys
case-*insensitive* properties (with getters/setters, *and key-like*)
with *separate* namespaces
(the storage space is effectively *doubled*)
(you can have a regular map, but with *special info* stored in properties, e.g. values or objects)
non-existent keys cause an *error* (if an attempt is made to read from one)
non-existent properties return a *blank string*
Last edited by jeeswg on 07 Sep 2019, 07:44, edited 4 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: objects: properties (static/dynamic)

Post by Helgef » 07 Sep 2019, 00:02

Static properties can be defined by preceding the property name with the separate keyword static. In that case, this refers to the class itself or a subclass.

for _, vProp in ["prop0", "prop1", "prop2"]
See :arrow: array.__enum .

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: objects: properties (static/dynamic)

Post by jeeswg » 07 Sep 2019, 06:31

Objects - Definition & Usage | AutoHotkey v2
https://lexikos.github.io/v2/docs/Objects.htm
Dynamic Properties

Property syntax and DefineProp can be used to define properties which compute a value each time they are evaluated
So a method-like property (with a descriptor specifying a getter and/or a setter) is called a dynamic property.
But what do you call the simple key-like property (that you read/write to, like a key, that uses the default getter/setter).
(What do you call a non-dynamic property?) (Bearing in mind the opposite of dynamic is static.)

Does the documentation actually explain what it means by a static property? Or does it just refer to it, assuming the reader knows what is meant.
Anyhow, here's an example. I might say that the use of 'static' creates a class property, and omitting it creates an instance property.
(What do you call a non-static property?) (I've called it an instance property.)

Code: Select all

MsgBox(MyClass.abc) ;123
MsgBox(MyClass.def) ;(blank)

obj := new MyClass
MsgBox(obj.abc) ;(blank)
MsgBox(obj.def) ;456

class MyClass
{
	static abc := 123
	def := 456
}
Re. __Enum, thanks, but in this case prop0 was deliberately a property that did not exist. And, I know that, for arrays, we can omit the Key parameter in AHK v2, but for script clarity it may be better to always use it. So you consistently have: one parameter = key, two parameters = key and value. Also, code reusability, between arrays and maps, is improved.
Last edited by jeeswg on 07 Sep 2019, 09:27, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: objects: properties (static/dynamic)

Post by Helgef » 07 Sep 2019, 09:00

Does the documentation actually explain what it means by a static property?
yes.
we can omit the Key parameter in AHK v2, but for script clarity it may be better to always use it.

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: objects: properties (static/dynamic)

Post by jeeswg » 07 Sep 2019, 09:32

- You will not find a good (current) documentation quote explaining a static property. (A lot of the time you can find good documentation quotes for things, but not this time.)
- Why did you post the Key quote?
- Got any answers for the OP's 3 questions? E.g. what would you call a 'non-dynamic' property?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: objects: properties (static/dynamic)

Post by Helgef » 07 Sep 2019, 10:17

- You will not find a good (current) documentation quote explaining a static property. (A lot of the time you can find good documentation quotes for things, but not this time.)
But there is, I guess you just don't want to put in any effort in finding it yourself.
- Why did you post the Key quote?
I ran out of steam.
what would you call a 'non-dynamic' property?
If the distinction were relevant, I'd call it just that. We might also occasionally refer to such a thing as a static/class or instance variable, as the documentation does.

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: objects: properties (static/dynamic)

Post by jeeswg » 07 Sep 2019, 12:50

But there is, I guess you just don't want to put in any effort in finding it yourself.
- That's a bit of an odd statement, I quote the documentation at approximately the same rate that you do.
- The closest mention: the documentation mentions instance and static/class 'variables', but doesn't state that they're *properties*. On this page:
Objects - Definition & Usage | AutoHotkey v2
https://lexikos.github.io/v2/docs/Objects.htm
- Something I view as sufficiently counterintuitive (calling a prop a var, and defining a prop inside a class using 'var := value' syntax) to warrant a clarification (it's a prop).

- (You still didn't explain why you posted the Key quote.)

- For brevity/clarity, e.g. in the example in the OP and in the documentation/tutorials, it would be useful to have a short term for 'non-dynamic' property, that everyone understands.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

lexikos
Posts: 9665
Joined: 30 Sep 2013, 04:07
Contact:

Re: objects: properties (static/dynamic)

Post by lexikos » 07 Sep 2019, 21:40

The manual says,
There are value properties and dynamic properties.
This is on the page for Object, the base type which implements both kinds of properties. COM wrapper objects do not have either type.

Post Reply

Return to “Ask for Help (v2)”