[v2.0-rc.1] Better error message for missing parentheses

Report problems with documented functionality
CtrlAltDel
Posts: 1
Joined: 29 Nov 2022, 12:40

[v2.0-rc.1] Better error message for missing parentheses

Post by CtrlAltDel » 29 Nov 2022, 12:53

Code like

Code: Select all

UserProfile := EnvGet "UserProfile"
results in the error message "Expected a String but got a Func", whereas the real error is that the EnvGet call is missing the parentheses. I think it would be great if the error message could be improved for the case of missing parentheses, in particular now that AHK 2 is new and many users converting their scripts will probably run into this kind of error.

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

Re: [v2.0-rc.1] Better error message for missing parentheses

Post by lexikos » 29 Nov 2022, 19:20

Although your request is fair, this is clearly not a bug. The code has valid syntax - as far as the program is concerned, missing parentheses is not the problem.

To understand why you get this message, try this code, which is perfectly legal and behaving correctly:

Code: Select all

f() {
	EnvGet := "Get the "
	UserProfile := EnvGet "UserProfile"
	return UserProfile
}
MsgBox f()
If you add % on the last line, it will behave the same on v1.

User avatar
thqby
Posts: 397
Joined: 16 Apr 2021, 11:18
Contact:

Re: [v2.0-rc.1] Better error message for missing parentheses

Post by thqby » 29 Nov 2022, 19:59

image.png
image.png (27.68 KiB) Viewed 1945 times
He probably wanted a hint like this.

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

Re: [v2.0-rc.1] Better error message for missing parentheses

Post by Helgef » 04 Dec 2022, 05:55

the real error is that the EnvGet call is missing the parentheses. I think it would be great if the error message could be improved for the case of missing parentheses, in particular now that AHK 2 is new and many users converting their scripts will probably run into this kind of error.
I disagree, you will make the mistake of trying to make a function call like this once, and then never again. So in all subsequent cases the error message is accurate.

Cheers.

CoolRaoul
Posts: 12
Joined: 27 Jul 2017, 04:23

Re: [v2.0-rc.1] Better error message for missing parentheses

Post by CoolRaoul » 22 Jul 2023, 05:33

Sorry to enter that old thread but I'm facing the same issue and not understand the given explanations.

(I'm currently trying to convert my AHK V1 scripts to V2 Syntax BTW)

A reply indicates that
"The code has valid syntax - as far as the program is concerned, missing parentheses is not the problem"
if the syntax is valid, why en error message is posted then? (and adding parentheses doesn't change anything for me, getting same error)

to assign the UserProfile variable with the value of the "UserProfile" env variable, what is the syntactically correct way to write it then?

neogna2
Posts: 586
Joined: 15 Sep 2016, 15:44

Re: [v2.0-rc.1] Better error message for missing parentheses

Post by neogna2 » 22 Jul 2023, 15:40

CoolRaoul wrote:
22 Jul 2023, 05:33
to assign the UserProfile variable with the value of the "UserProfile" env variable, what is the syntactically correct way to write it then?

Code: Select all

UserProfile := EnvGet("UserProfile")

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

Re: [v2.0-rc.1] Better error message for missing parentheses

Post by lexikos » 23 Jul 2023, 21:24

It is a runtime error, not a syntax error. A variable followed by a string is valid syntax for concatenating strings. If the variable contains an object (if and) when the expression is evaluated, you get a runtime error.

jaawn
Posts: 2
Joined: 22 Nov 2023, 09:30

Re: [v2.0-rc.1] Better error message for missing parentheses

Post by jaawn » 22 Nov 2023, 13:22

I think at the very least, this may warrant some clarifications in the documentation and error messages. I ran into it with code similar to the following (this is a contrived example, the real code does more stuff before/after function.Call()):

Code: Select all

doStuff(function) {
	function.Call()
}

#!^Numpad0::
{
	;The Send call here generates the error unless you add parentheses around the string parameter.
	doStuff(() => Send "stuff to type{Enter 2}" )
}
The Concepts and Conventions>Functions, Function Call Statements, and Functions doc pages all use language that describe situations where the parentheses "can be omitted", but I think more accurate (or at least clear) language would be along the lines of "parentheses are required for a function call unless..."

Additionally, since it is so common to find examples using the space syntax for a function call outside the context of these pages, it is very, very easy to come away with the impression that that is the "correct" way to call a function in any case. Also, if the first time you read those pages are when you are not facing this issue, it's easy to miss the importance of that detail. If error messages don't provide some pathway toward finding the right sections in the documentation that cover this, you're left fumbling in the dark and resorting to reading the docs top to bottom until you find the relevant section (which I just did today haha).

Post Reply

Return to “Bug Reports”