Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Expression commands should have formal syntax versions as well



  • Please log in to reply
6 replies to this topic
robertcollier4
  • Members
  • 141 posts
  • Last active: Jun 28 2019 01:05 AM
  • Joined: 11 Nov 2012
To me the biggest frustrating thing in AHK_L is that you are forced to have to use expression syntax for many of the internal commands such as "FileAppend" - this means that they are unable to get data directly from an object property. It makes it very frustrating when trying to use complex data sets such as Objects of Objects. Thus whenever you want to use the data from an object - you have to take a performance hit and use an intermediary variable - which also makes the code less readable and less pretty.
 
For example, if I have the array:
PersonArray := Object("Ricardo", Object("Age", 38, "Occupation", "Singer"), "Lucy", Object("Age", 36, "Occupation", "Housewife"), "Ethel", Object("Age", 33, "Occupation", "Bridgeplayer"))
Now I want to write this data to a file in tab-delimited format, DOES NOT WORK:
For key, value in PersonArray {
	FileAppend, %key%`t%value.Age%`t%value.Occupation%`n, output.txt
}
Instead I have to use temporary variables which is much less readable:
For key, value in PersonArray {
	TempAge := value.Age
	TempOcc := value.Occupation
	FileAppend, %key%`t%TempAge%`t%TempOcc%`n, output.txt
}
Ideally, I propose that for every "Expression syntax function" there is also an internal "Formal syntax function" such as following:
For key, value in PersonArray {
	FileAppend(key . "`t" . value.Age . "`t" . value.Occupation . "`n", "output.txt")
}
Out of the 3 examples - you can see how much more readable the last one is. In my opinion, everything should be in formal syntax. Having to type % percent signs around all the variable names is also harder on the fingers and increases coding time and finger fatigue.

Coco
  • Members
  • 697 posts
  • Last active: Oct 31 2015 07:26 PM
  • Joined: 27 Jul 2012

No need to store those in temporary variables, you can actually write it like this:

For key, value in PersonArray {
	FileAppend, % key "`t" value.age "`t" value.Occupation "`n", output.txt
}


robertcollier4
  • Members
  • 141 posts
  • Last active: Jun 28 2019 01:05 AM
  • Joined: 11 Nov 2012
Thanks that helps. But I still recommend that everything should be ALSO provided in standard function syntax notation as an AHK_L internally provided function.
Keep the old expression commands for backwards compatability, but give the user the option of also having formal functions using formal notation for all internal expressions.
Obviously this would mean reserving those function names in the function namespace, but I don't think that should be an issue.
 
If(FileAppend("some text", "output.txt")) {
	// some code
}
Is better than:
FileAppend, some text, output.txt
If ErrorLevel {
	// some code
}
It also negates the need to use AutoTrim workarounds if you happen to encounter a variable that holds a leading space or tab. For example I was working on a clipboard history saving script using IniWrite - and it would break when saving the clipboard to INI when the clipboard var contained strings having leading spaces or tabs. If I had IniWrite() instead in formal function notation, it would not have been a problem and I would have saved time.

The expression syntax is quirky and has a high learning curve which limits the growth of AutoHotkey IMHO. I'm fine with it staying for my old scripts - but would like to have FileAppend() and IniWrite() as well so I can use formal function syntax in my new scripts.

Coco
  • Members
  • 697 posts
  • Last active: Oct 31 2015 07:26 PM
  • Joined: 27 Jul 2012

Option to use function syntax for AHK commands is already available for AHK v. 2 which is still in the alpha stages. I would love to have it already available for the current AHK version(v. 1.1.12). wink.png



Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
✓  Best Answer

Robert, it seems to me that you have your terminology backwards.

 

This is expression syntax:

FileAppend(key . "`t" . value.Age . "`t" . value.Occupation . "`n", "output.txt")

This is not expression syntax:

FileAppend, %key%`t%TempAge%`t%TempOcc%`n, output.txt

It is often referred to as command syntax, since FileAppend is generally referred to as a command, not a function (though in general terms, it is a function).

 

Ideally, I propose that for every "Expression syntax function" there is also an internal "Formal syntax function" such as following:

 

Judging by your example, you mean to suggest that for every command, there is an equivalent function.

 

 

Note that all of your examples already work in v2.  I suggest you give it a try, as I have no intention of porting these changes to v1.



faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
@robertcollier4 see "Commands as functions" http://www.autohotke...s-as-functions/ and a newer version here http://www.autohotkey.com/board/topic/91667-commands-as-functions/

guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
as everyone else said, your terminology is wrong. basically you are asking to be able to call the FileAppend command instead as a function. this will be possible in AHKv2. it doesn't look like faqbot's link has a wrapper for FileAppend, but you can look at that library and write your own basic wrapper if you need to

finally, "expression syntax" is different from "literal text", and Coco gave an example of how to use expressions in commands, by preceeding the command parameter with a single %

also

in your first example, you tried to dereference an object.key with the double % signs. this is not possible in v1. i'm not sure of the current state of AHKv2, but i think it will be possible to do that there as well. last i read, you can dereference any expression at all within the %%:
 
MsgBox, object.key= %object.key%
MsgBox, 2+2= %2+2%
youll need to check the latest v2 changes to see whats proposed
http://l.autohotkey.net/v2-changes.htm