inconsistent or-maybe results

Report problems with documented functionality
tamo
Posts: 11
Joined: 11 Jul 2019, 04:23

inconsistent or-maybe results

21 Mar 2024, 23:00

"??" (or-maybe) cannot be used with object.property

but %"object.property"% ?? fails without errors.

I think that's at least inconsistent.

Code: Select all

o := { key: "value" }
MsgBox(o.key) ; "value"
MsgBox(%"o.key"% ?? "default") ; "default"
MsgBox(o.key ?? "default") ; Syntax error
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: inconsistent or-maybe results

22 Mar 2024, 00:31

This is essentially a duplicate of a:=%5%??77 will not throw. There is no difference between %"nonexistentvariablename"% and %"any invalid variable name"%, by design. It is not valid to dynamically reference a variable which does not exist, regardless of whether the name you are passing would be valid for a variable. No variable exists with the given name, therefore the right branch of ?? is used.

If %Expr% was to differentiate between valid and invalid variable names, it would throw an error for %"o.key"% because this is not a valid variable name. I do not think that is what you are expecting or wanting to happen.

The documentation states that %Expr% is "dereference" or "name substitution", where "the value of the sub-expression Expr is used as the name or partial name of a variable or property." You might have interpreted it to mean that Expr can be a string like "o.key", but that is not what it means. An example of valid use is o.%"key"% - i.e. Expr provides the name of the property. "o.key" is not the name of the property, but an arbitrary expression. If the operator was capable of evaluating arbitrary expressions within a string, it would be the "arbitrary expression evaluator", not "name substitution".

o.key ?? "default" is valid in v2.1-alpha.2+. The documentation for v2.0 states that the left operand of ?? must be a variable, which o.key is not.
tamo
Posts: 11
Joined: 11 Jul 2019, 04:23

Re: inconsistent or-maybe results

23 Mar 2024, 01:37

Thanks for your detailed explanation! I understand more than I expected.
And I'm really excited to hear about the improvement planned in v2.1.
AutoHotkey v2 has been my lifesaver these days! I appreciate your work every day!

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 94 guests