assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

The `?.` operator in a world without `undefined`

Open trusktr opened this issue 4 years ago • 1 comments

Continuing from https://github.com/AssemblyScript/assemblyscript/issues/1973#issuecomment-880706201 to this new topic so as not to mix topics, here's an idea.

What if ?. returns null when something is null? This would work for objects and strings, but currently not for primitives which can't be made nullable.

But the general idea is that if something is Foo | null for example, then something?.foo() would be an expression that returns null if something is null.

In TypeScript, ?. returns undefined. But maybe in AssemblyScript it would be fine for it to return null instead?

In TypeScript, I never compare directly to null or undefined. I write things like if (foo) instead of things like if (foo !== null), and so my own code would not have any problems with ?. returning null instead of undefined while making AssemblyScript code portable with TypeScript.

TS/VSCode would have undefined under the hood, but as long as null isn't checked for directly (which I think people should avoid) then I think it could work out and the difference would not be noticed.

trusktr avatar Jul 18 '21 18:07 trusktr

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!

github-actions[bot] avatar Aug 17 '21 23:08 github-actions[bot]