Re: Optional chaining syntax but with the "mice" operator ?
Since somebody asked me already elsewhere about the expected precedence of the operator, this would be my answer: ```js const result = await dbQuery(data) wrote: > absolutely, I'm working with PostgreSQL these days and indeed for any > promise/awaited result this pattern looks like a win, and while it's > targeting a limitation of the chaining one, it can be used in various other > cases where knowing the initial result is more important than just falling > back to "_dunnoWhatHappenedThere_" > > On Fri, Sep 6, 2019 at 12:24 AM Michael Luder-Rosefield < > rosyatran...@gmail.com> wrote: > >> Another pattern it could be useful in is with, say, nosql dbs where >> something might be an object or id reference: >> >> ``` >> const fooId = foo> ``` >> >> On Thu, 5 Sep 2019, 23:03 Andrea Giammarchi, >> wrote: >> >>> Another use case that I believe will be common is the following one: >>> >>> ```js >>> // current state of the art >>> const result = dbQuery(data)?.rows ?? 'did it just failed or what?'; >>> >>> // VS the "mice operator" >>> const result = dbQuery(data)>> >>> // if it was rows >>> if (Array.isArray(result)) >>> console.log(result); >>> else if (result instanceof Error) >>> console.error(result.message); >>> else >>> console.warn(`unexpected result: ${result}`); >>> ``` >>> >>> Ideally, the "mice" should grant chaining up to its latest presence, but >>> I wouldn't know right now how to reference to it ... >>> >>> ```js >>> // if no ?? is needed, this might work >>> const result = dbQuery(data)>> >>> // if ?? is needed, no idea how to back-reference the latest successfull >>> "mice" result >>> ``` >>> >>> >>> >>> >>> On Thu, Sep 5, 2019 at 11:44 PM Tab Atkins Jr. >>> wrote: >>> On Thu, Sep 5, 2019 at 2:39 PM Andrea Giammarchi wrote: > > This is basically a solution to a common problem we have these days, where modules published in the wild might have a `default` property, to support ESM logic, or not. > > ```js > // current optional chaining logic > const imported = exported?.default ?? exported; > > // my "mice operator" proposal > const imported = exported>>> > ``` > > Semantically speaking, not only `>>> also points at its previous value in case the chaining didn't work. > > Beside the basic example, the "mice operator" might save CPU cycles when it comes to involving more complex expressions, i.e. > > ```js > // current "solution" > const thing = require('thing')?.default ?? require('thing'); > > // mice operator > const thing = require('thing')>>> > ``` > > This is also easily tranpilable, so kinda a no-brainer for modern dev tools to bring in. > > TL;DR specially for cases where an accessed property should fallback to its source, this operator might save both typing and CPU time whenever it's needed. I find it a rather curious pattern, that I'd never seen before! Is it used in anything besides this ESM-compat thing you're talking about? (Saving CPU cycles is not a convincing argument; it's trivial to write such a line over two declarations and avoid any expensive recomputations.) ~TJ >>> ___ >>> es-discuss mailing list >>> es-discuss@mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >> ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Optional chaining syntax but with the "mice" operator ?
absolutely, I'm working with PostgreSQL these days and indeed for any promise/awaited result this pattern looks like a win, and while it's targeting a limitation of the chaining one, it can be used in various other cases where knowing the initial result is more important than just falling back to "_dunnoWhatHappenedThere_" On Fri, Sep 6, 2019 at 12:24 AM Michael Luder-Rosefield < rosyatran...@gmail.com> wrote: > Another pattern it could be useful in is with, say, nosql dbs where > something might be an object or id reference: > > ``` > const fooId = foo ``` > > On Thu, 5 Sep 2019, 23:03 Andrea Giammarchi, > wrote: > >> Another use case that I believe will be common is the following one: >> >> ```js >> // current state of the art >> const result = dbQuery(data)?.rows ?? 'did it just failed or what?'; >> >> // VS the "mice operator" >> const result = dbQuery(data)> >> // if it was rows >> if (Array.isArray(result)) >> console.log(result); >> else if (result instanceof Error) >> console.error(result.message); >> else >> console.warn(`unexpected result: ${result}`); >> ``` >> >> Ideally, the "mice" should grant chaining up to its latest presence, but >> I wouldn't know right now how to reference to it ... >> >> ```js >> // if no ?? is needed, this might work >> const result = dbQuery(data)> >> // if ?? is needed, no idea how to back-reference the latest successfull >> "mice" result >> ``` >> >> >> >> >> On Thu, Sep 5, 2019 at 11:44 PM Tab Atkins Jr. >> wrote: >> >>> On Thu, Sep 5, 2019 at 2:39 PM Andrea Giammarchi >>> wrote: >>> > >>> > This is basically a solution to a common problem we have these days, >>> where modules published in the wild might have a `default` property, to >>> support ESM logic, or not. >>> > >>> > ```js >>> > // current optional chaining logic >>> > const imported = exported?.default ?? exported; >>> > >>> > // my "mice operator" proposal >>> > const imported = exported>> > ``` >>> > >>> > Semantically speaking, not only `>> also points at its previous value in case the chaining didn't work. >>> > >>> > Beside the basic example, the "mice operator" might save CPU cycles >>> when it comes to involving more complex expressions, i.e. >>> > >>> > ```js >>> > // current "solution" >>> > const thing = require('thing')?.default ?? require('thing'); >>> > >>> > // mice operator >>> > const thing = require('thing')>> > ``` >>> > >>> > This is also easily tranpilable, so kinda a no-brainer for modern dev >>> tools to bring in. >>> > >>> > TL;DR specially for cases where an accessed property should fallback >>> to its source, this operator might save both typing and CPU time whenever >>> it's needed. >>> >>> I find it a rather curious pattern, that I'd never seen before! Is it >>> used in anything besides this ESM-compat thing you're talking about? >>> >>> (Saving CPU cycles is not a convincing argument; it's trivial to write >>> such a line over two declarations and avoid any expensive >>> recomputations.) >>> >>> ~TJ >>> >> ___ >> es-discuss mailing list >> es-discuss@mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Optional chaining syntax but with the "mice" operator ?
Another pattern it could be useful in is with, say, nosql dbs where something might be an object or id reference: ``` const fooId = foo wrote: > Another use case that I believe will be common is the following one: > > ```js > // current state of the art > const result = dbQuery(data)?.rows ?? 'did it just failed or what?'; > > // VS the "mice operator" > const result = dbQuery(data) > // if it was rows > if (Array.isArray(result)) > console.log(result); > else if (result instanceof Error) > console.error(result.message); > else > console.warn(`unexpected result: ${result}`); > ``` > > Ideally, the "mice" should grant chaining up to its latest presence, but I > wouldn't know right now how to reference to it ... > > ```js > // if no ?? is needed, this might work > const result = dbQuery(data) > // if ?? is needed, no idea how to back-reference the latest successfull > "mice" result > ``` > > > > > On Thu, Sep 5, 2019 at 11:44 PM Tab Atkins Jr. > wrote: > >> On Thu, Sep 5, 2019 at 2:39 PM Andrea Giammarchi >> wrote: >> > >> > This is basically a solution to a common problem we have these days, >> where modules published in the wild might have a `default` property, to >> support ESM logic, or not. >> > >> > ```js >> > // current optional chaining logic >> > const imported = exported?.default ?? exported; >> > >> > // my "mice operator" proposal >> > const imported = exported> > ``` >> > >> > Semantically speaking, not only `> also points at its previous value in case the chaining didn't work. >> > >> > Beside the basic example, the "mice operator" might save CPU cycles >> when it comes to involving more complex expressions, i.e. >> > >> > ```js >> > // current "solution" >> > const thing = require('thing')?.default ?? require('thing'); >> > >> > // mice operator >> > const thing = require('thing')> > ``` >> > >> > This is also easily tranpilable, so kinda a no-brainer for modern dev >> tools to bring in. >> > >> > TL;DR specially for cases where an accessed property should fallback to >> its source, this operator might save both typing and CPU time whenever it's >> needed. >> >> I find it a rather curious pattern, that I'd never seen before! Is it >> used in anything besides this ESM-compat thing you're talking about? >> >> (Saving CPU cycles is not a convincing argument; it's trivial to write >> such a line over two declarations and avoid any expensive >> recomputations.) >> >> ~TJ >> > ___ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Optional chaining syntax but with the "mice" operator ?
Another use case that I believe will be common is the following one: ```js // current state of the art const result = dbQuery(data)?.rows ?? 'did it just failed or what?'; // VS the "mice operator" const result = dbQuery(data) wrote: > On Thu, Sep 5, 2019 at 2:39 PM Andrea Giammarchi > wrote: > > > > This is basically a solution to a common problem we have these days, > where modules published in the wild might have a `default` property, to > support ESM logic, or not. > > > > ```js > > // current optional chaining logic > > const imported = exported?.default ?? exported; > > > > // my "mice operator" proposal > > const imported = exported > ``` > > > > Semantically speaking, not only ` points at its previous value in case the chaining didn't work. > > > > Beside the basic example, the "mice operator" might save CPU cycles when > it comes to involving more complex expressions, i.e. > > > > ```js > > // current "solution" > > const thing = require('thing')?.default ?? require('thing'); > > > > // mice operator > > const thing = require('thing') > ``` > > > > This is also easily tranpilable, so kinda a no-brainer for modern dev > tools to bring in. > > > > TL;DR specially for cases where an accessed property should fallback to > its source, this operator might save both typing and CPU time whenever it's > needed. > > I find it a rather curious pattern, that I'd never seen before! Is it > used in anything besides this ESM-compat thing you're talking about? > > (Saving CPU cycles is not a convincing argument; it's trivial to write > such a line over two declarations and avoid any expensive > recomputations.) > > ~TJ > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Optional chaining syntax but with the "mice" operator ?
On Thu, Sep 5, 2019 at 2:39 PM Andrea Giammarchi wrote: > > This is basically a solution to a common problem we have these days, where > modules published in the wild might have a `default` property, to support ESM > logic, or not. > > ```js > // current optional chaining logic > const imported = exported?.default ?? exported; > > // my "mice operator" proposal > const imported = exported ``` > > Semantically speaking, not only ` points at its previous value in case the chaining didn't work. > > Beside the basic example, the "mice operator" might save CPU cycles when it > comes to involving more complex expressions, i.e. > > ```js > // current "solution" > const thing = require('thing')?.default ?? require('thing'); > > // mice operator > const thing = require('thing') ``` > > This is also easily tranpilable, so kinda a no-brainer for modern dev tools > to bring in. > > TL;DR specially for cases where an accessed property should fallback to its > source, this operator might save both typing and CPU time whenever it's > needed. I find it a rather curious pattern, that I'd never seen before! Is it used in anything besides this ESM-compat thing you're talking about? (Saving CPU cycles is not a convincing argument; it's trivial to write such a line over two declarations and avoid any expensive recomputations.) ~TJ ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Optional chaining syntax but with the "mice" operator ?
This is basically a solution to a common problem we have these days, where modules published in the wild might have a `default` property, to support ESM logic, or not. ```js // current optional chaining logic const imported = exported?.default ?? exported; // my "mice operator" proposal const imported = exported___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss