-
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
Why not operator overloading in Typescript? #6936
Comments
Can you provide a specific use case of what you are trying to accomplish? In most of the use cases I can conceive of operator overloading, it would require some sort of run-time emit, which is very anti-pattern for TypeScript. |
For example: vector classes.
Alike: https://github.com/kushal-likhi/operator-overloading-js But with differences:
|
See #5407 |
I want operator overload in typescript! i write a fuckin game engine in typescript if there is operator overloading in it. |
@gdefraiteur check out TurboScript with operator overload and pointers which compile to WebAssembly. Even better for game engine 😎 |
@nidin why not, but the thing is that i like to code with visual studio, and i'm not sure your language is handled in visual studio node project. :/ the interest of typescript for me is that it is usable in such an ide. |
@gdefraiteur IDE support can be achieved by developing a small plugins. Between TurboScript was a prototype. Prototype phase ended, now I am working on a detailed language spec. https://github.com/turboscriptlang/spec |
@nidin well, then i hope your project will go well and that you ll make those required plugins. ^^. |
@gdefraiteur of course 🙂 |
To add my 2 cents: I would like to be able to define a composition operator for function composition like |
+1 |
You can use Proxy in ES6 to overload brasket operator []. Here example. As i know this will work in typescript [too].(https://www.typescriptlang.org/docs/handbook/advanced-types.html). |
I don't actually think deviating from standard JS and implementing operator overloading is a good idea, but here's a thing I haven't heard anyone talk about: As state in here, one of the problems with operator overloading proposals for ECMScript is the fact that it would require extensive type checks, something that could maybe be facilitated by TS's type system. I don't have a profound enough understanding on the tsc compiler, but I assume it may hold some extra type information that could perhaps make such an implementation feasible. |
@RyanCavanaugh I'd like to make my point in the original discussion about this, which is now locked. Is there be any possibility of it getting unlocked? |
Is that possible to write a custom typescript transform? |
Can't believe there is not operator overloading in TypeScript, being the best place to support such feature, right now you need to define a special methods to add two instances of anything , the most simple use case is adding {x, y} coordinates. Currently you have to use a custom method, lets say is called "add": const a = new Point(1, 2)
const b = new Point(3, 4)
const c = new Point(4, 5)
const result = a.add(b).add(c) instead of just being way more understandable code such as const a = new Point(1, 2)
const b = new Point(3, 4)
const c = new Point(4, 5)
const result = a + b + c |
Yea, there is no reason for not doing it. HaXe e.g. compiles to JavaScript aswell and also supports operator overloading. And so does AssemblyScript, which compiles directly from TypeScript to WebAssembly. Every game developer will thank the implementator of operator overloading. It probably isn't even that hard, just some simple recursive ast conversions from |
@kungfooman, but it isn't that simple. In fact operator overloading with type inference is hard. And as far as I know, TS doesn't use the Hindley-Milner type system. In effect that means that operator overloading would throw away all of TS's guarantees. For operator overloading to be of any use, the type system needs to be sound and capeable of advanced type inference. TS's type system is neither. Maybe that kind of proposal made more sense for Flow, if it weren't for the fact that they also try not to alter JavaScript in any way. But because Flow tries to be sound instead of pracitcal, it has all the problems TS tries to avoid. Of course there are other dynamic languages that do support operator overloading, namely Python. But they don't have to worry about type safety. Java doesn't have operator overloading because of all those complications (and some other reasons). |
Why is it hard exactly, I assume is due inheritance and generics? |
@Ivanca, because the compiler has to evaluate the order of execution and must then determine if the types are compatible. And the receiving type might not actually be the one you are expecting. For example, when adding to interface types and the compiler determines in which order they should be added. |
if A + A always results in A it would be good enough for most use cases, meaning implementing a subset of overloading where all elements must be the exact same type and the result is that same type as well. The compatibility is the lesser issues of them all, you can just prefix the methods and be done with it, e.g. |
Hello. Why not operator overloading in Typescript? I can not found discussion about.
The text was updated successfully, but these errors were encountered: