Page 1 of 1

inconsistent or-maybe results

Posted: 21 Mar 2024, 23:00
by tamo
"??" (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

Re: inconsistent or-maybe results  Topic is solved

Posted: 22 Mar 2024, 00:31
by lexikos
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.

Re: inconsistent or-maybe results

Posted: 23 Mar 2024, 01:37
by tamo
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!