Unquoted strings, *sigh*

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pls2000
Posts: 3
Joined: 29 Jan 2021, 23:58

Unquoted strings, *sigh*

30 Jan 2021, 00:06

The forum rules don't allow me to express my feelings when I discovered that the line

Code: Select all

qWin = WinExists("A")
sets qWin to WinExists("A"), with not error either then, or when the value is later used in WinActivate

I hope a future version can do away with unquoted strings.
braunbaer
Posts: 483
Joined: 22 Feb 2016, 10:49

Re: Unquoted strings, *sigh*

30 Jan 2021, 03:55

There are two different formats for strings that AHK uses.
The command style uses unquoted strings, where variables have to be enclosed in quotes while all the other text is used "as is"
The expression style needs quotes to enclose string constants.

The assignment operator "=" uses the command style, while the assignment operator ":=" uses the expression style. Sometimes, the use of unquoted strings is very convenient, but in your case, you must write:

Code: Select all

qWin := WinExists("A")
I agree that it would be a good idea to issue a WARNING when a command style assignment is made and the string looks very much like an expression style string.
pls2000
Posts: 3
Joined: 29 Jan 2021, 23:58

Re: Unquoted strings, *sigh*

30 Jan 2021, 04:33

Even after fixing the function name, it still took it as a string
qWIn = WinExist("A")
assigns WinExist("A") to the variable

I'm kind of a language purist, and the whole thing about two types of assignment, two types of string that aren't interchangeable with some contexts requiring one kind and some the other just rubs me the wrong way.

Oh well, I'll learn eventurally.
gregster
Posts: 9113
Joined: 30 Sep 2013, 06:48

Re: Unquoted strings, *sigh*

30 Jan 2021, 05:04

Like already indicated, you have to use the "walrus" operator for assigning in expression mode:

Code: Select all

:=
That means qWIn := WinExist("A")

and not a single = without a :
(the = assignment operator was kept for backwards compatibility in v1.1; it's the so called legacy mode which is considered deprecated, isn't recommended to use anymore, and has been removed in AHK v2 alpha - to use it with a function call like above, you would have to do this: qWIn = % WinExist("A") , forcing an expression)
gregster
Posts: 9113
Joined: 30 Sep 2013, 06:48

Re: Unquoted strings, *sigh*

30 Jan 2021, 05:07

pls2000 wrote:
30 Jan 2021, 04:33
I'm kind of a language purist
In this case, AHK 1.x is probably the wrong language for you. Perhaps v2 alpha is more to your liking.
But in 1.1, both assignment operators, := and =, will stay for the rest of its existence, I am pretty sure.
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Unquoted strings, *sigh*

30 Jan 2021, 08:08

pls2000 wrote:
30 Jan 2021, 04:33
I'm kind of a language purist, and the whole thing about two types of assignment, two types of string that aren't interchangeable with some contexts requiring one kind and some the other just rubs me the wrong way.
In case it makes you feel better, this is one of the biggest pain points for those learning AHK and remains one of most frustrating aspects/quirks of the language. I don’t think anyone would purposely set up this dual syntax approach when designing a language. It was the result of adding powerful new functionality (expressions) without breaking backward compatibility. AHK v2 suggested by gregster is the first official attempt at revising the language without holding to the backward compatibility requirement.
Pepineros
Posts: 45
Joined: 16 Apr 2018, 17:26
Location: Ireland

Re: Unquoted strings, *sigh*

30 Jan 2021, 08:18

pls2000 wrote:
30 Jan 2021, 00:06
The forum rules don't allow me to express my feelings...
Well said :mrgreen: I have felt the same way.

I'm sure you've figured this out but it helps to choose one method and stick with it. If you like the simple = and don't mind prefacing expressions with %, go for that. If you like walruses and don't mind wrapping literal strings in quotes, go for :=.

Both work in every situation as long as you know which one you're dealing with.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: DecimalTurn, macromint, peter_ahk and 338 guests