Skip to content
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

Operator Overloads #5407

Closed
Antony-Jones opened this issue Oct 26, 2015 · 3 comments
Closed

Operator Overloads #5407

Antony-Jones opened this issue Oct 26, 2015 · 3 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Duplicate An existing issue was already created Out of Scope This idea sits outside of the TypeScript language design constraints

Comments

@Antony-Jones
Copy link

It would simplify a lot of use cases if we could override relational, equality, additive, and Multiplicative operators.

My initial thoughts on how this would work is that functions would replace the operators with functions when compiling to JavaScript.

class MyClass {
    constructor() {
    }

    public Operator > (value: any):boolean {
        // compare value
    }
}

var myClass = new MyClass();

if(myClass > otherValue){
    // Do stuff
}

Becomes:

var MyClass = (function () {
    function MyClass() {
    }
    MyClass.prototype.greaterThan = function (value) {
        // compare value
    };
    return MyClass;
})();
var myClass = new MyClass();
if (myClass.greaterThan(otherValue) {
    // do Stuff
}
@DanielRosenwasser
Copy link
Member

One thing we avoid doing is type-driven emit. The idea is that TypeScript types have no bearing at runtime - the code is as close to the analogous JavaScript that you would have written. Given that, substituting in a method call for an operator would go against that.

Apart from that, this is related to #2319, so I'm going to close this as a duplicate.

@DanielRosenwasser DanielRosenwasser added Duplicate An existing issue was already created Declined The issue was declined as something which matches the TypeScript vision Out of Scope This idea sits outside of the TypeScript language design constraints labels Oct 26, 2015
@nitinbansal1989
Copy link

i have written an ORM framework in typescript. Its still new and only supports mysql currently. Its architecture is based on entity framework.
https://www.npmjs.com/package/es-entity
Enabling Operator Overloading will support the creating of 'LINQ' like queries.
You can add this feature as a optional extension like Decorators feature with experimental implementation and compiler option. Hope to see this in typescript.

@jay3sh
Copy link

jay3sh commented Aug 11, 2017

I'm developing numpy like library in typescript (bluemath). Operator overloading will significantly improve the interface of my NDArray class.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Duplicate An existing issue was already created Out of Scope This idea sits outside of the TypeScript language design constraints
Projects
None yet
Development

No branches or pull requests

4 participants