About traditional assignments

Discuss the future of the AutoHotkey language
john_c
Posts: 493
Joined: 05 May 2017, 13:19

About traditional assignments

Post by john_c » 24 Feb 2021, 11:07

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.
lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: About traditional assignments

Post by lexikos » 25 Feb 2021, 19:50

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.
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: About traditional assignments

Post by john_c » 28 Feb 2021, 09:02

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.
lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: About traditional assignments

Post by lexikos » 28 Feb 2021, 19:56

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}).
TAC109
Posts: 1099
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: About traditional assignments

Post by TAC109 » 28 Feb 2021, 20:19

@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.
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe
lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: About traditional assignments

Post by lexikos » 01 Mar 2021, 05:45

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.
TAC109
Posts: 1099
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: About traditional assignments

Post by TAC109 » 01 Mar 2021, 14:50

Apologies, I’m clearly not paying enough attention. :thumbdown:
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe
Post Reply

Return to “AutoHotkey Development”