Does Object/Map have Longland/Shorthand

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
FanaticGuru
Posts: 1905
Joined: 30 Sep 2013, 22:25

Does Object/Map have Longland/Shorthand

Post by FanaticGuru » 30 Jan 2023, 14:49

I have been making the switch from v1 to v2 as I assume many people have been lately with the official v2 release.

Really, really like the consistency of all functions and all expressions and getting rid of having to remember where to use "" and %. Although using stuff like Obj.%var% still seems a little off as that reminds me of the hacky protoarrays but I am coming around to it.

GUI changes are taking some learning but GUI was messing already with vVar and gSubroutine so it is worth relearning. But I wasted quite a bit of time because I did not have (*) instead of just () for my functions called by the GUI. It seems like it would just assume any number of parameters is ok when you don't care about any of the parameters. I assume it is to catch errors but it seems it will cause more errors than catch. A tutorial with lots of example code for GUI would be useful.

The other big change relearning is Objects. It took a while to get that the generic object has been split into different types of objects. My take is there are now three main ones: arrays, maps, and objects. In my mind all those are objects but objects now mean properties/methods. Again a tutorial with lots of practical example code for defining, accessing, looping, etc would be great. Just figuring out using for prop, val in Obj.OwnProps() took me quite a bit of googling. I still don't know if you can loop through the actual properties and not just the names of the properties.

I am wondering if I am missing some shorthand and longhands as shown below:

Code: Select all

MyArray := Array("One", "Two") ; Longhand
MyArray := ["One", "Two"] ; Shorthand
MsgBox 'MyArray[1] `n' MyArray[1]

MyObj := {One:1, Two:2} ; Shorthand
;~ MyObj := Object(One:1, Two:2) ; No Longhand
MsgBox 'MyObj.One `n' MyObj.One


MyMap := Map("One",1,"Two",2) ; Longhand
;~ MyMap := ["One":1,"Two":2] ; No Shorthand
MsgBox 'MyMap["One"] `n' MyMap["One"]

I get the Array longhand and shorthand.
Does Object have a longhand that lets you define multiple properties all at once?
Does Map have a shorthand that lets you define multiple maps all at once?

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Does Object/Map have Longland/Shorthand

Post by mikeyww » 02 Feb 2023, 13:03

Some additional ways: Object

User avatar
FanaticGuru
Posts: 1905
Joined: 30 Sep 2013, 22:25

Re: Does Object/Map have Longland/Shorthand

Post by FanaticGuru » 02 Feb 2023, 16:08

mikeyww wrote:
02 Feb 2023, 13:03
Some additional ways: Object

I appreciate the effort but I would have not ask a questions without at least attempting to find it in the help document.

I get there might not be a shorthand for MyMap := Map("One",1,"Two",2). Something just do not have shorthands.

but it seems strange I cannot find a longhand function that does this: MyObj := {One:1, Two:2}. It seems very much like Object() should allow parameters to create and define properties with values in one function.

If anyone can post an alternative for one of the above, that would be great. It is very possible I am just overlooking something.

My switch to v2 is coming along but almost all the learning curve has been the changes in Objects and GUI. I assume the new Object structure allows for more power which I am ok with as I am willing to put the time in to learn but it is definitely not as newbie friendly. There is a lot of stuff now about properties, methods, extending, base, prototype, etc. Reminds me of discovering RegEx; powerful but not easy.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Does Object/Map have Longland/Shorthand

Post by mikeyww » 02 Feb 2023, 16:33

OK. I'm not an expert in this, but if you provide some clarifications, it might help the real experts to answer your question.

1. Can you provide a specific example of your vision for the longhand version?

2. Why do you want a longhand or longer approach than what is available?

User avatar
FanaticGuru
Posts: 1905
Joined: 30 Sep 2013, 22:25

Re: Does Object/Map have Longland/Shorthand

Post by FanaticGuru » 02 Feb 2023, 18:07

mikeyww wrote:
02 Feb 2023, 16:33
OK. I'm not an expert in this, but if you provide some clarifications, it might help the real experts to answer your question.

1. Can you provide a specific example of your vision for the longhand version?

2. Why do you want a longhand or longer approach than what is available?

I would like to have a longhand for this: {One:1, Two:2} because at the top of my script when I create and type my variables, I can do something like this: Object(One:1, Two:2)

And I would like to have a shorthand for this: Map("One",1,"Two",2) so that when I create a temporary Map in the meat of my code then it is compact.

Basically, I like to do stuff longhand like this: MyArray := Array("One", "Two") at the top of my script for objects that are going to be used throughout my script, so it is easy to see the type because now type is important as now v1 objects have been split into objects, arrays, and maps which behave differently from each other. But then I like to do stuff shorthand like this: for k, v in ["One","Two"] in my script when the value is not being saved and is just a temporary creation. So, longhand for assignments Shorthand for no assignment, only used here.

I would like to have array, object, and map syntax symmetry. But understand there might be some shorthand conflicts but don't understand the lack of a longhand.

It seems like what I would like does not exist for whatever reason. So now I am going to look at what I can maybe figure out with Prototype or some other technique for modifying existing functionality. I mean I was pretty bummed about no support for multi-dimension maps until I found someone had extended it: viewtopic.php?t=74028 to support multi-dimensional maps.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Does Object/Map have Longland/Shorthand

Post by mikeyww » 02 Feb 2023, 19:34

Thank you for explaining it. Although I understand what you wrote, I see no functional point to what you have described. It seems like you just want a different style or word to achieve something that is already readily achievable. Others may have some more insightful comments than mine!

User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Does Object/Map have Longland/Shorthand

Post by kczx3 » 02 Feb 2023, 20:41

Using an editor that provides intelligence, such as VSCode, is helpful for knowing the variable’s contained type. Aside from that, I think it’s pretty obvious that if you see the curly braces then that’s an Object since nothing else can use that structure. Another option that I’ve used is to use camel case with single letter prefixes to help identify the type.

oObject
mMap
aArray
pPointer
hHwnd

And so on

User avatar
FanaticGuru
Posts: 1905
Joined: 30 Sep 2013, 22:25

Re: Does Object/Map have Longland/Shorthand

Post by FanaticGuru » 03 Feb 2023, 13:48

kczx3 wrote:
02 Feb 2023, 20:41
oObject
mMap
aArray
pPointer
hHwnd

Yea, I go back and forth on that. In the past I have only used it when dealing with various COM objects but now that Map, Array, and Object behave differently, I might start using that for anything except for primitives (which if I am using my terminology right is strings and numbers). "g" for Gui objects is another one. AutoHotkey is becoming a more robust language so I guess I need to take on the trappings of that and keep track of types.

I need to make a complete list of what Type can return for built-in classes.

Also, I realize there is no functional difference between a longhand and shorthand syntax. I just wanted to make sure I was not missing something, which apparently, I am not. There is no longhand for { } or shorthand for Map().

But on a good note, it helped me to figure out my first use of Prototype.

Code: Select all

ObjectMod()
ObjectMod()
{
	Object.Prototype.DefineProp("__New", {Call:__NewObject})
	__NewObject(this, params*)
	{
		for index, param in params
			if Mod(index,2)
				Prop := param
			else
				this.%Prop% := param
	}
}

MyObj := Object("One", 1, "Two", 2) ; new with parameters
MyObj2 := Object() ; make sure no parameters still works
MyObj2.Three := 3

MsgBox MyObj.Two "`n" MyObj2.Three

That may not look like much, but it took me a lot of starring at the help documents to figure that out.

Not that I would necessarily use this, but it was quite satisfying to be able to modify a built-in command.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

iseahound
Posts: 1427
Joined: 13 Aug 2016, 21:04
Contact:

Re: Does Object/Map have Longland/Shorthand

Post by iseahound » 03 Feb 2023, 15:11

Keep in mind that Apps Hungarian

xMouse, yMouse
_privatevar, _internalvar
pPointer
hWnd
xs (where the s makes x plural)
like natural kinds

is much different from Systems Hungarian

boolVariable
oObject
aArray
lList
uint32var
like data types

https://en.wikipedia.org/wiki/Hungarian_notation#Systems_Hungarian_vs._Apps_Hungarian

Post Reply

Return to “Ask for Help (v2)”