proposal-extended-numeric-literals icon indicating copy to clipboard operation
proposal-extended-numeric-literals copied to clipboard

Would this enable unit conversions?

Open samuelgoto opened this issue 7 years ago • 9 comments

By reading the text, it would seem like it would, but wanted to sanity check:

let a = 1_cm;
let b = 1_m;
console.log(`${a.plus(b)} meters`);
// prints 1.01_m

I'm assuming that by the virtue of these things being extensible through the definition of

function _cm() {
  // do some magical thing in user land
}
function _m() {
  // do other magical things in user land
}

Is that a correct interpretation of this feature?

samuelgoto avatar Mar 20 '18 12:03 samuelgoto

Yes, you could use it for this sort of thing.

littledan avatar Mar 20 '18 16:03 littledan

@littledan @samuelgoto I'm curious if either of you could write a desugaring of the example given?

rwaldron avatar Mar 23 '18 21:03 rwaldron

1_cm desugars into roughly _cm(Object.freeze({string: "1", number: 1})

littledan avatar Mar 27 '18 14:03 littledan

@littledan sorry, I should've been more specific: I'm looking for a desugaring that would demonstrate how @samuelgoto's example would work, ie. a complete script that can be executed and which will show the output that's expected.

rwaldron avatar Mar 27 '18 17:03 rwaldron

I'm sorry, I'm having trouble understanding the goal here. It seems pretty easy to make a class that has a plus method and return instances from functions like _cm. What will this exercise show us about the extensible literals proposal?

littledan avatar Mar 27 '18 20:03 littledan

It seems pretty easy to make a class that has a plus method and return instances from functions like _cm.

I'm asking you to write that out in JS code that can be executed, and which will show this example working as you say it will.

What will this exercise show us about the extensible literals proposal?

This exercise will demonstrate the amount of user code that's necessary to make these deceptively simple examples work the way you say they will.

rwaldron avatar Apr 06 '18 17:04 rwaldron

@rwaldron

class UnitOfMeasure {
  constructor(mm) {
    this.mm = mm;
  }
  plus(other) {
    return new UnitOfMeasure(other.mm + this.mm);
  }
  toString() {
    return (this.mm / 1000).toString();
  }
}

function _cm({ number: cm }) {
  return new UnitOfMeasure(cm * 10);
}

function _m({ number: m }) {
  return new UnitOfMeasure(m * 1000);
}

let a = _cm(Object.freeze({string: "1", number: 1}));
let b = _m(Object.freeze({string: "1", number: 1}));
console.log(`${a.plus(b)} meters`);

michaelficarra avatar Apr 06 '18 22:04 michaelficarra

While I see 1_cm.plus(1_m), it's very natural to ask what happened about 1_cm + 2_cm? Do we have follow-on operator overloading proposal?

hax avatar Jul 09 '18 09:07 hax

@hax Operator overloading has been a topic of informal discussion among various TC39 members for a while. This proposal is one part of an operator overloading/extensible literals/value types triad. @keithamus recently presented something, and I expect we'll see something more soon about operator overloading. However, I think we'll develop it in a separate repository, and have them be multiple separate complementary proposals that interact well.

littledan avatar Jul 09 '18 12:07 littledan