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

Closed
ghost opened this Issue Feb 6, 2016 · 17 comments

Comments

Projects
None yet
10 participants
@ghost
Copy link

ghost commented Feb 6, 2016

Hello. Why not operator overloading in Typescript? I can not found discussion about.

@kitsonk

This comment has been minimized.

Copy link
Contributor

kitsonk commented Feb 6, 2016

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.

@ghost

This comment has been minimized.

Copy link
Author

ghost commented Feb 6, 2016

For example: vector classes.

  var p0 = new Point(0, 0);
  var p1 = new Point(100, 0);
  var p = p0 + p1; //Operator with overload
  var d = 1 +! 2; //Strict operator (not overloaded)
  point.prototype.__add = function(left){
    //overload operator
  }
  point.prototype.__assign = function(left){
    //overload operator
  }
  point.prototype.__get = function(key){
    //overload array op
  }
  point.prototype.__set = function(key, value){
    //overload array op
  }

Alike: https://github.com/kushal-likhi/operator-overloading-js

But with differences:

  • Strict operators that can not be overloaded (with "!" postfix).
  • Built-in operator overloading, instead of functions with overloading.
a!["name"] = b; //not overloaded
a["name"] = b; //overloaded
a =! b; //not overloaded
a = b; //overloaded
a +=! b; //not overloaded
a += b; //overloaded
@RyanCavanaugh

This comment has been minimized.

Copy link
Member

RyanCavanaugh commented Feb 6, 2016

See #5407

@gdefraiteur

This comment has been minimized.

Copy link

gdefraiteur commented Mar 21, 2017

I want operator overload in typescript!

i write a fuckin game engine in typescript if there is operator overloading in it.

@nidin

This comment has been minimized.

Copy link

nidin commented May 10, 2017

@gdefraiteur check out TurboScript with operator overload and pointers which compile to WebAssembly. Even better for game engine 😎

@gdefraiteur

This comment has been minimized.

Copy link

gdefraiteur commented Jul 31, 2017

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

@nidin

This comment has been minimized.

Copy link

nidin commented Jul 31, 2017

@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

@gdefraiteur

This comment has been minimized.

Copy link

gdefraiteur commented Jul 31, 2017

@nidin well, then i hope your project will go well and that you ll make those required plugins. ^^.

@nidin

This comment has been minimized.

Copy link

nidin commented Jul 31, 2017

@gdefraiteur of course 🙂

@Ytrog

This comment has been minimized.

Copy link

Ytrog commented Apr 17, 2018

To add my 2 cents: I would like to be able to define a composition operator for function composition like const composedFunc = f >> g; or a pipe operator like const result = textBox.value |> trim;

@LinBoLen

This comment has been minimized.

Copy link

LinBoLen commented May 28, 2018

+1

@munrocket

This comment has been minimized.

Copy link

munrocket commented Jul 28, 2018

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

@GarkGarcia

This comment has been minimized.

Copy link

GarkGarcia commented Dec 28, 2018

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.

@GarkGarcia

This comment has been minimized.

Copy link

GarkGarcia commented Dec 28, 2018

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

@LinBoLen

This comment has been minimized.

Copy link

LinBoLen commented Dec 29, 2018

Is that possible to write a custom typescript transform?

@Ivanca

This comment has been minimized.

Copy link

Ivanca commented Jan 3, 2019

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

This comment has been minimized.

Copy link

kungfooman commented Jan 5, 2019

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 a + b to a.add(b) etc. There are quite some examples for that in pure JavaScript and acorn JS parser, e.g. PaperScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment