tc39 / proposal-extended-numeric-literals

Extensible numeric literals for JavaScript
https://tc39.github.io/proposal-extended-numeric-literals/
72 stars 18 forks source link

Would this enable unit conversions? #11

Open samuelgoto opened 6 years ago

samuelgoto commented 6 years ago

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?

littledan commented 6 years ago

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

rwaldron commented 6 years ago

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

littledan commented 6 years ago

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

rwaldron commented 6 years ago

@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.

littledan commented 6 years ago

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?

rwaldron commented 6 years ago

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.

michaelficarra commented 6 years ago

@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`);
hax commented 6 years ago

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?

littledan commented 6 years ago

@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.