pass object key ByRef?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

pass object key ByRef?

12 Jun 2019, 01:55

- It's not the most-needed feature, but it would be potentially useful if you could pass an object key ByRef.
- E.g. for the example below, you can't use object keys in the Swap function, and so a separate ObjSwap function is needed.

Code: Select all

obj := {}
obj.key1 := "1"
obj.key2 := "2"

MsgBox, % obj.key1 " " obj.key2 ;1 2

Swap(obj.key1, obj.key2)
MsgBox, % obj.key1 " " obj.key2 ;1 2 (no change)

ObjSwap(obj, "key1", "key2")
MsgBox, % obj.key1 " " obj.key2 ;2 1

Swap(ByRef var1, ByRef var2)
{
	temp := var1
	var1 := var2
	var2 := temp
}

ObjSwap(obj, key1, key2)
{
	temp := obj[key1]
	obj[key1] := obj[key2]
	obj[key2] := temp
}
- I'm posting in case anybody has seen something like this be done in a programming language.
- And, to ask if such a thing would be possible in AHK. Either a function that can treat an object key as ByRef, or an operator to pass an object key as ByRef. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
just me
Posts: 9575
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: pass object key ByRef?

12 Jun 2019, 02:59

AFAIR this has already been discussed more than one time. In AHK (1.1) Object.Key is an expression retrieving the current value of Object.Key. It's not possible to pass the result of an expression ByRef.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: pass object key ByRef?

12 Jun 2019, 11:14

One potential solution, would be functions that accept a string like "obj.key" and could dereference it.
This could be useful for a small number of built-in/custom functions, but some Deref functionality would be needed.

Another solution, would be an object type that stores "obj.key" in one of its keys, and if a function detects an object with that 'PassVar' (or whatever) class, it could handle it.

Anyhow, this is not a priority of mine, but worth considering.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
YoucefHam
Posts: 372
Joined: 24 Aug 2015, 12:56
Location: Algeria
Contact:

Re: pass object key ByRef?

12 Jun 2019, 12:10

jeeswg wrote:
12 Jun 2019, 11:14
One potential solution, would be functions that accept a string like "obj.key" and could dereference it.
hi jeeswg,
I am learning from you post about RegEx So when I see RegEx challenge I go for it,

I tried this, and I am not expert in objects stuff

Code: Select all

obj := {}
obj.key1 := "1"
obj.key2 := "2"

Swap("obj.key1", "obj.key2")

MsgBox, % obj.key1 " " obj.key2 ;2 1

Swap(var1, var2) ;no need for ByRef here :(
{
	Loop, 2
		RegExMatch(var%A_Index%, "^(.*?)\.(.*?)$",var%A_Index%)
	temp := ObjRawGet(%var11%, var12)
	ObjRawSet(%var11%, var12, ObjRawGet(%var21%, var22))
	ObjRawSet(%var21%, var22, temp)
}
:wave: There is always more than one way to solve a problem. ;)
just me
Posts: 9575
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: pass object key ByRef?

12 Jun 2019, 16:05

jeeswg -> request wrote:- And, to ask if such a thing would be possible in AHK. Either a function that can treat an object key as ByRef, or an operator to pass an object key as ByRef.
jeeswg -> possible solution wrote:One potential solution, would be functions that accept a string like "obj.key" and could dereference it.
:roll:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: pass object key ByRef?

12 Jun 2019, 23:24

- @YoucefHam: Your RegEx and ObjRawGet/ObjRawSet code looks good.
- For 'abc.def' I would probably do the following:

Code: Select all

;AHK v1 (notice 'O)'):
RegExMatch("abc.def", "O)^([^.]+)\.([^.]+)$", oMatch)
MsgBox, % oMatch.1 " " oMatch.2

;AHK v2:
RegExMatch("abc.def", "^([^.]+)\.([^.]+)$", oMatch)
MsgBox(oMatch.1 " " oMatch.2)
- Note: I use 'O)' (object mode), because it's more forwards compatible.

- Here's a prototype, but it's not intended for everyday AutoHotkey use. It can only handle objects if the object is in the global namespace.

Code: Select all

q:: ;test swap
var1 := "1"
var2 := "2"
MsgBox, % var1 " " var2 ;1 2
Swap(var1, var2)
MsgBox, % var1 " " var2 ;2 1

obj := {}
obj.key1 := "1"
obj.key2 := "2"
MsgBox, % obj.key1 " " obj.key2 ;1 2
Swap("obj.key1", "obj.key2")
MsgBox, % obj.key1 " " obj.key2 ;2 1

obj1 := {}
obj2 := {}
obj1.key1 := "1"
obj2.key2 := "2"
MsgBox, % obj1.key1 " " obj2.key2 ;1 2
Swap("obj1.key1", "obj2.key2")
MsgBox, % obj1.key1 " " obj2.key2 ;2 1

obj1 := {}
obj1.key1 := "1"
var2 := "2"
MsgBox, % obj1.key1 " " var2 ;1 2
Swap("obj1.key1", var2)
MsgBox, % obj1.key1 " " var2 ;2 1

obj1 := {}
obj1.key1 := {}
obj1.key1.key1 := "1"
var2 := "2"
MsgBox, % obj1.key1.key1 " " var2 ;1 2
Swap("obj1.key1.key1", var2)
MsgBox, % obj1.key1.key1 " " var2 ;2 1

SwapTest()

SwapTest()
{
	MsgBox, % "this swap will fail"
	objnew := {}
	objnew.key1 := "1"
	objnew.key2 := "2"
	MsgBox, % objnew.key1 " " objnew.key2 ;1 2
	Swap("objnew.key1", "objnew.key2")
	MsgBox, % objnew.key1 " " objnew.key2 ;2 1
}

Swap(ByRef var1, ByRef var2)
{
	global
	local objname, temp, value1, value2, varnum
	Loop, 2
	{
		if IsByRef(var%A_Index%) ;variable
			value%A_Index% := var%A_Index%
		else ;string
		{
			temp := StrSplit(var%A_Index%, ".")
			objname := temp.RemoveAt(1)
			value%A_Index% := %objname%[temp*]
		}
	}
	Loop, 2
	{
		varnum := (A_Index = 1) ? 2 : 1
		if IsByRef(var%A_Index%) ;variable
			var%A_Index% := value%varnum%
		else ;string
		{
			temp := StrSplit(var%A_Index%, ".")
			objname := temp.RemoveAt(1)
			%objname%[temp*] := value%varnum%
		}
	}
}
- @just me: I'm concerned both about how and why you made your helpful(?) point/emoji.
- There's no real argument to be against the functionality per se.
- Whether you want it for AHK or not is another matter.
- What I'm really interested in is parallels in other languages. And the desire for the functionality described, or for workarounds. The general principle is this: avoiding writing multiple functions, by forcing an object key into a function, the *key* rather than its value.
- The most interesting idea might be: a unary operator, that would output the key's value as a temporary ByRef variable, and then retrieve that variable's value, and store it in the key.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
just me
Posts: 9575
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: pass object key ByRef?

13 Jun 2019, 02:41

Hi jeeswg,

it's just:
One potential solution, would be functions that accept a string like "obj.key" ...
is obviously not a solution for the original request
Either a function that can treat an object key as ByRef, or an operator to pass an object key as ByRef.

Also, assume-global functions are problematic. I think it's better to store the values in two temporary variables, call the Swap()function, and restore the swapped values afterwards (KISS).
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: pass object key ByRef?

02 Jul 2019, 07:24

- One idea could be something like:
- objkey := ObjGetKey(obj, "key")
- Whereby objkey := "value" would be equivalent to obj.key := "value".
- An 'object key stored as a variable' class.

Code: Select all

;store key as a variable (possible syntax):
obj := {}
obj.key := "old value"
MsgBox, % obj.key ;old value

mykey := ObjGetKey(obj, "key")
mykey := "new value"
MsgBox, % obj.key ;new value
- Re. functions that accept a string like "obj.key" (as a ByRef object key, not a ByVal string):
- That *is* a solution. (For some functions, but not all.)
- 'a function that can treat an object key as ByRef' (via strings)
- 'an operator to pass an object key as ByRef' (operator *or quotes* or other syntax)

- (I wouldn't use assume-global functions in any solution.)

- Part of the reason this idea was suggested, was KISS.
- I'd seen variable names as strings in AutoIt's Assign function.
- To avoid bugs resulting from using temporary variables (e.g. re. 'static').
- To reduce codes lines.
- To avoid needing separate functions for variables/objects.
- Potentially, for functional-style programming.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Descolada, Mateusz53, peter_ahk, Rohwedder and 172 guests