Page 1 of 1

About traditional assignments

Posted: 24 Feb 2021, 11:07
by john_c
As far as I know, traditional assignments won't work in AHK2.

My thread is just a small friendly remark that in some cases these assignments are very useful.

Here is a regex to remove C-style comments.

Code: Select all

regex = (["'][^"']?\/\*.*?\*\/[^"']*["'])|\/\*(?:.|\R)*?\*\/
There are quotation marks there, and by using the traditional mode, you don't need to escape them.

Escaping the quotation marks is error-prone for one people, boring for other, but the most important thing is that a regex with unescaped quotation marks can be tested online, for example, at https://regexr.com/. And such testing is a "must have" feature for complex regular expressions.

What I'm talking about here is may be already obvious for some people, but I personally discovered the usefulness of traditional assignments just recently.

Re: About traditional assignments

Posted: 25 Feb 2021, 19:50
by lexikos
Traditional assignments are a much more common cause of confusion. And no, they don't exist in v2 now and still won't in the future.

Copy-pasting a regex pattern to a traditional assignment, you must still be careful of ` and %.

If you use a v2 continuation section with (`, I think you only need to avoid ) at the start of a line. Your regex can span multiple lines, which can be useful (perhaps in combination with the x option) in complex cases.

Code: Select all

regex := "
(`
(["'][^"']?\/\*.*?\*\/[^"']*["'])|\/\*(?:.|\R)*?\*\/
)"
MsgBox regex
Personally, I test my regex patterns within the platform where I will be using them. I believe there are scripts that help with this.

Re: About traditional assignments

Posted: 28 Feb 2021, 09:02
by john_c
Well, I'm fine with this. However, just a quick idea, "we" (I'm not an AHK dev) can introduce literal assignments instead, so there will be no need to escape quotation marks or any other characters where it doesn't make sense. And on the other hand, it won't be possible to include a variable reference or a function call as a part of the value that is currently assigned; this is both a drawback and a feature.

As you see, literal assignments are very close to traditional ones, but the idea is simpler and the name is better.

Re: About traditional assignments

Posted: 28 Feb 2021, 19:56
by lexikos
What you have described is just a less flexible version of traditional assignment. Many alternatives to the v1 traditional assignment (or removing it) were discussed and rejected years ago.

A literal assignment would restore unwanted duality to the syntax, while being much less useful than some of the alternative suggestions due to the limited context in which it can be used. Other ideas that have been discussed, such as verbatim/raw/heredoc strings, are more widely usable, such as in a function parameter. Even the existing continuation section syntax is usable anywhere.

Also, "literal assignment" is already used by some to refer to traditional assignment. I don't use either name in the documentation; I prefer "legacy assignment".

One idea I don't recall considering is to add a continuation section option to convert it into a quoted string, similar to a suggestion made in 2011.

Code: Select all

regex :=
  (Quote
    (["'][^"']?\/\*.*?\*\/[^"']*["'])|\/\*(?:.|\R)*?\*\/
  )
(Text was my first thought, but it should be clear that the option merely adds quotes, and does not affect the interpretation of keys by Send (like {Text}).

Re: About traditional assignments

Posted: 28 Feb 2021, 20:19
by TAC109
@john_c Referring to your first post, in v2 literals can be enclosed with either " or ', so by using ' for literal enclosure there is no need to double the embedded " marks.

Re: About traditional assignments

Posted: 01 Mar 2021, 05:45
by lexikos
Doubling quote marks is not the way to escape them in v2. The OP is using both quote mark types, i.e. ["'] for "double quote or single quote", so swapping from double quotes to single quotes will not help in the slightest.

Re: About traditional assignments

Posted: 01 Mar 2021, 14:50
by TAC109
Apologies, I’m clearly not paying enough attention. :thumbdown: