Open samuelgoto opened 6 years ago
Yes, you could use it for this sort of thing.
@littledan @samuelgoto I'm curious if either of you could write a desugaring of the example given?
1_cm
desugars into roughly _cm(Object.freeze({string: "1", number: 1})
@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.
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?
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
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`);
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 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.
By reading the text, it would seem like it would, but wanted to sanity check:
I'm assuming that by the virtue of these things being extensible through the definition of
Is that a correct interpretation of this feature?