Re: The map-lookup-forgiveness problem

On Tue, 2023-02-07 at 12:29 -0800, Dimitre Novatchev wrote:
> Addressing the "difficulty to debug" issue, this reminds me of C#
> where
> there are both the "." (member access)  and "?."  (null-conditional)
>   operators.
> 
> https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and
> -
> 

This has recently been added to JavaScript too as i understand it.

There is also the null-condensing operator ?? in which a ?? b is like
our (a, b)[1]. So i think we should avoid using ?? to mean something
similar but subtly different; || is bad enough already :)

JavaScript has ?. as null-safe property access, and this has widespread
support. See
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#browser_compatibility
function printCustomerCity(customer) {
  const customerCity = customer?.city ?? "Unknown city";
  console.log(customerCity);
}

which uses both of these operators.

As a side-note, there are also nullish assignment operators, e.g.
  a ??= e;
is like XPath's
  a := if (a) then a else e
except we can't update variables of course, so this is pernicious
shadowing...

and ||= and &&=, neither of which we could use :)

But i think we probably should avoid using ?? to mean the same as
JavaScript and C# ".?" - this will especially confuse SaxonJS users :)
and lead to bugs that are hard to find.

I do however think a safe dereference operator is a good idea, and if
it wasn't for JS using ?? in that way, i'd like ?? for it.

Maybe we should use ‽ instead :) or allow [.]? to work that way, so
  e1[.]?e2
returns () if e1 is empty, e1?e2 if e1 is a map, and an error
otherwise.

liam

-- 
Liam Quin, https://www.delightfulcomputing.com/
Available for XML/Document/Information Architecture/XSLT/
XSL/XQuery/Web/Text Processing/A11Y training, work & consulting.
Barefoot Web-slave, antique illustrations:  http://www.fromoldbooks.org

Received on Tuesday, 7 February 2023 21:16:37 UTC