-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overloading Operators Syntax #19859
Comments
Duplicate #5407 and others; please search before logging new suggestions |
I did search and saw those others, and wasn't sure where to add this suggestion, the described implementation is different in a few ways. Using static member functions guarantees the the function will exist at runtime, (potentially using an My intention in sharing this implementation of a feature that has already been proposed few times is to open up more ideas as to how this feature might be implemented without having negative effects on JavaScript emit and runtime. emit (with instanceof check): class MyNumber {}
MyNumber.add(lhs, rhs) {
if (lhs instanceof MyNumber && rhs instanceof MyNumber) {
// code
} else {
return lhs + rhs
}
} This way durning runtime even if non A more general from of an operator could be: class `class-name` {
operator `operator-name`(lhs: `class-name`, rhs: `any-class`): `class-name` {
// code
}
} example:class MyNumber {
operator add(lhs: MyNumber, rhs: MyNumber): MyNumber {
// code 1
}
operator add(lhs: MyNumber, rhs: Number): MyNumber {
// code 2
}
} emits:class MyNumber {}
MyNumber.add(lhs, rhs) {
if (lhs instanceof MyNumber && rhs instanceof MyNumber) {
// code 1
} else if (lhs instanceof MyNumber && rhs instanceof Number) {
// code 2
} else {
return lhs + rhs
}
} |
If you read #5407 you should have seen that the first comment on it explained that this is something Typescript is not going to do because it requires type-driven emit. Based on your example: class MyNumber {}
MyNumber.add = function(lhs, rhs) {
// code
}
let a = new MyNumber(5)
let b = new MyNumber(6)
let c = 7
a + b // would emit MyNumber.add(a, b)
a + c // would emit a + c
|
Arithmetic Operators
syntax:
emits:
Comparison Operators
syntax:
emits:
The text was updated successfully, but these errors were encountered: