TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Suggestion: Extension methods

Open RyanCavanaugh opened this issue 12 years ago • 170 comments

Allow a declarative way of adding members to an existing type's prototype

Example:


class Shape {
 // ...
}

/* ...elsewhere...*/
extension class Shape { // Syntax??
    getArea() { return /* ... */; }
}

var x = new Shape();
console.log(x.getArea()); // OK

RyanCavanaugh avatar Jul 15 '14 16:07 RyanCavanaugh

@alonbardavid see https://github.com/Microsoft/TypeScript/issues/9#issuecomment-74302592 for why call site rewriting really, truly, really just does not work.

RyanCavanaugh avatar Aug 01 '16 07:08 RyanCavanaugh

@RyanCavanaugh To take your example code from https://github.com/Microsoft/TypeScript/issues/9#issuecomment-74302592 I don't entirely see why some amount of call site rewriting can't work at all. For instance could we do something like:

// Setup
class Rectangle {
  constructor(public width: number, public height: number) {}
}
// Do not be distracted by exact syntax here
extension function getArea(this s: Rectangle ) {
  return s.width * s.height;
}
// end setup

// Challenge course begins
interface HasArea {
  getArea(): number;
}

function fn1(x: HasArea) {
  console.log(x.getArea());
}
fn1(new Rectangle(10, 10));

var a: any = Math.random() > 0.5 ? new Rectangle(3, 3) : { getArea: function() { return 3; } };
console.log(a.getArea());

var s = new Rectangle(5, 5);
var f = s.getArea.bind(s);
console.log(f);

class Square {
  constructor(public size: number) { }
  getArea() { return this.size * this.size; }
}
var box: Square|Rectangle = /* ... anything ...*/
console.log(box.getArea());

box.getArea.call(box);

var boxes: HasArea[] = [new Square(5), new Rectangle(10, 10)];
boxes.forEach(b => {
  console.log(b.getArea());
});

First gets compiled down to typescript like:

// Setup
class Rectangle {
  constructor(public width: number, public height: number) {}
}

class __ExtendRectangleBacking
{
    constructor (public Instance: Rectangle)
    {
        this.getArea = __ExtendRectangleBacking.prototype.getArea.bind (this);
    }

    getArea ()
    {
        debugger;
        if (this.Instance["getArea"] !== undefined)
        {
            return (this.Instance["getArea"].apply (this.Instance, Array.prototype.slice.call (arguments)));
        }

        var s = this.Instance;
        return (s.width * s.height);
    }
}

var __ExtendRectangle = function (Target: Rectangle)
{
    return (new __ExtendRectangleBacking (Target));
};
// end setup

// Challenge course begins
interface HasArea {
  getArea(): number;
}

function fn1(x: HasArea)
{
  console.log(x.getArea());
}

fn1(__ExtendRectangle (new Rectangle(10, 10)));

var a: any = Math.random() > 0.5 ? __ExtendRectangle (new Rectangle(3, 3)) : { getArea: function() { return 3; } };
console.log(a.getArea());

var s = new Rectangle(5, 5);
var f = __ExtendRectangle (s).getArea.bind(s);
console.log(f);

class Square {
  constructor(public size: number) { }
  getArea() { return this.size * this.size; }
}

// Worst case:
var box: Square | Rectangle = new Square (137);

console.log(__ExtendRectangle (<Rectangle>/*Probably*/box).getArea());

__ExtendRectangle (<Rectangle>/*Probably*/box).getArea.call(box);

var boxes: (HasArea | Rectangle) [] = [new Square(5), new Rectangle(10, 10)];

boxes.forEach(b => {
  console.log(__ExtendRectangle (<Rectangle>/*Probably*/b).getArea());
});

And then emited as javascript like:

// Setup
var Rectangle = (function () {
    function Rectangle(width, height) {
        this.width = width;
        this.height = height;
    }
    return Rectangle;
}());
var __ExtendRectangleBacking = (function () {
    function __ExtendRectangleBacking(Instance) {
        this.Instance = Instance;
        this.getArea = __ExtendRectangleBacking.prototype.getArea.bind(this);
    }
    __ExtendRectangleBacking.prototype.getArea = function () {
        debugger;
        if (this.Instance["getArea"] !== undefined) {
            return (this.Instance["getArea"].apply(this.Instance, Array.prototype.slice.call(arguments)));
        }
        var s = this.Instance;
        return (s.width * s.height);
    };
    return __ExtendRectangleBacking;
}());
var __ExtendRectangle = function (Target) {
    return (new __ExtendRectangleBacking(Target));
};
function fn1(x) {
    console.log(x.getArea());
}
fn1(__ExtendRectangle(new Rectangle(10, 10)));
var a = Math.random() > 0.5 ? __ExtendRectangle(new Rectangle(3, 3)) : { getArea: function () { return 3; } };
console.log(a.getArea());
var s = new Rectangle(5, 5);
var f = __ExtendRectangle(s).getArea.bind(s);
console.log(f);
var Square = (function () {
    function Square(size) {
        this.size = size;
    }
    Square.prototype.getArea = function () { return this.size * this.size; };
    return Square;
}());
// Worst case:
var box = new Square(137);
console.log(__ExtendRectangle(box).getArea());
__ExtendRectangle(box).getArea.call(box);
var boxes = [new Square(5), new Rectangle(10, 10)];
boxes.forEach(function (b) {
    console.log(__ExtendRectangle(b).getArea());
});
  • Allows use on uncertain types. See: https://github.com/Microsoft/TypeScript/issues/9#issuecomment-236507167
  • Can be used on union types

If the extension method returns s than accessing width gets converted to:

box.SomeMethodThatReturnsS ().width

Gets converted to:

__ExtendRectangle(box).SomeMethodThatReturnsS ().Instance.width

Any thoughts?

Naharie avatar Aug 08 '16 20:08 Naharie

You've "cheated" here

var a: any = Math.random() > 0.5 ? new Rectangle(3, 3) : { getArea: function() { return 3; } };
console.log(a.getArea());

The declared type of a is any. There's no justification to wrap the call to new Rectangle in a __ExtendRectangle call here but not wrap the same call on the line var s = new Rectangle(5, 5);. They're indistinguishable from the compiler's perspective.

The GC implications of this are terrifying - you're causing something like 5 extra allocations per function call even in places where 0 should be needed.

RyanCavanaugh avatar Aug 09 '16 17:08 RyanCavanaugh

Okay. On point number one or "cheating" just adjust it so that items only get wrapped when the extension method gets called. I am guessing something like this:

// Setup
class Rectangle {
  constructor(public width: number, public height: number) {}
}

class __ExtendRectangleBacking
{
    constructor (public Instance: Rectangle)
    {
        this.getArea = __ExtendRectangleBacking.prototype.getArea.bind (this);
    }

    getArea ()
    {
        if (this.Instance["getArea"] !== undefined)
        {
            return (this.Instance["getArea"].apply (this.Instance, Array.prototype.slice.call (arguments)));
        }

        var s = this.Instance;
        return (s.width * s.height);
    }
}

// Challenge course begins
interface HasArea {
  getArea(): number;
}

function fn1(x: HasArea)
{
  console.log(x.getArea());
}

fn1(new Rectangle(10, 10));

/* Okay. I admit I don't see how to get around this one: */
var a: any = Math.random() > 0.5 ? new Rectangle(3, 3) : { getArea: function() { return 3; } };
console.log(a.getArea());

var s = new Rectangle(5, 5);
var f = __ExtendRectangle (s).getArea.bind(s);
console.log(f);

class Square {
  constructor(public size: number) { }
  getArea() { return this.size * this.size; }
}

// Worst case:
var box: Square | Rectangle = new Square (137);

console.log(__ExtendRectangle (<Rectangle>/*Probably*/box).getArea());

__ExtendRectangle (<Rectangle>/*Probably*/box).getArea.call(box);

var boxes: (HasArea | Rectangle) [] = [new Square(5), new Rectangle(10, 10)];

boxes.forEach(b => {
  console.log(__ExtendRectangle (<Rectangle>/*Probably*/b).getArea());
});

Thanks for pointing that out. Ahh well. I was just hoping that extension methods might be possible in TypeScript and If possible suggest a way to do it.

Naharie avatar Aug 09 '16 22:08 Naharie

I think for those that really want extension methods it could be done as an add-in or a preprocessor

shmuelie avatar Aug 11 '16 16:08 shmuelie

@SamuelEnglard

Do any such things already exist?

Naharie avatar Aug 11 '16 21:08 Naharie

@KingDavid12 in theory #6508 could eventually allows this. In the short term the TS language service could be used to process the syntax tree and do the very transformation suggested above in a step before the compiler runs. If such a system currently exists I don't know, but I vaguely remember people talking about such things in other issues.

shmuelie avatar Aug 12 '16 20:08 shmuelie

I mean, if it could be accomplished correctly through a syntactic transform, we'd do it. The problem is that it can't.

RyanCavanaugh avatar Aug 12 '16 20:08 RyanCavanaugh

Based on this article, [(http://perfectionkills.com/extending-native-builtins/)] extension methods/(properties?) should be syntactic sugar for a "static" object, with the same semantics as C#. This ensures that extensions follow the SOLID rules and don't allow existing objects to be modified

Example

static class StringExtensions {
    public static isUpper(this value: string): boolean {
        ... logic;
    }
}

var myString: string = "Hello World";
var result: boolean = myString.isUpper();

Compiled

var StringExtensions = {
    isUpper: function(value) {
        ...logic;
    }
}

var myString = "Hello World";
var result = StringExtensions.isUpper(myString);

Considerations

  • extension classes should be open ended
  • extension method parameters should work with existing types, type aliases, unions, and interfaces

MrMatthewLayton avatar Oct 18 '16 12:10 MrMatthewLayton

@series0ne your comment suggests you haven't read the thread. This has been brought up and explained why it doesn't work several times

RyanCavanaugh avatar Oct 18 '16 15:10 RyanCavanaugh

If you want to see the closest real thing in TS/JS of extensions, look at RxJS. They combine extending with granular importing of functions but it's obviously very controlled and wouldn't work automatically for say ... an interface.

electricessence avatar Nov 03 '16 03:11 electricessence

So, I'm honestly really surprised that this has been talked to death and nothing has been worked out yet. I'm also surprised this was labeled 'out of scope'. That is very short-sighted, especially since extensions belong in TypeScript more than they belong in C# for all the same reasons that TypeScript decided to use a structural type-system in the first place.

This is gonna be a long one, and I apologize for that, but I think it has to be long to set up the case properly. I can see a lot of confusion as to the "what" and "why" for extensions.

TypeScript and Structural Typing

If I'm going to make a case for extensions, I need to make sure everyone knows what problem they are trying to solve. Yep, they solve a problem that exists in TypeScript today; they're not just a "nice thing to have". They facilitate a kind of programming that can be extremely powerful and useful. I think the best way to do this is to teach everyone the problem that structural typing solved in TypeScript's youth and how that leads to extensions.

When the powers that be started to work on TypeScript, they almost immediately ran into a problem: JavaScript is very promiscuous with the structure of its objects. In particular, two objects in JavaScript can have entirely different prototypes, but still have the same properties and much of the same behavior. If these were to bounce their way into TypeScript code through the same entry-point, it would need the equipment to deal with them.

Let's take a look at a very simple kind of structure, the 2D vector: { x: number, y: number } Just how many ways can you think of obtaining such a structure in TypeScript? Here are few!

// Importing one from a third-party library.
import VectorPro from 'lib/vector-pro';

// Making your own vector class.
class MyVector {
  constructor(public x: number, public y: number) { }
  /* ... other stuff ... */
}

// Coming in from your library's public API.
export function gimmieVectorsNomNom(v: { x: number, y: number }) { /* do noms */ }

// A raw JavaScript object.
let v1 = { x: 0, y: 0 };

Now, imagine writing a library where all those types can find their way into your code. TypeScript solved this problem by allowing you to wrangle them all into an interface based on their common structure.

interface VectorLike {
  x: number;
  y: number;
}

But you have a new problem. While you can handle the structure of a vector, you can't easily attach behavior to it. But you are a clever object-oriented programmer. Wrappers are a perfect solution!

class VectorWrapper<T extends VectorLike> implements VectorLike {
  get x(): number { return this.source.x; }
  set x(v: number) { this.source.x = v; }
  get y(): number { return this.source.y; }
  set y(v: number) { this.source.y = v; }
  
  constructor(public source: T) {}
  
  length(): number { /* length stuff */ }
  add(other: { x: number, y: number }): this { /* add stuff, return this */ }
  scale(scalar: number): this { /* scaling stuff, return this */ }
}

All done! Except you're not. If you're wrapping VectorLike objects into this thing, you now have a new problem. You have to begin unwrapping them whenever you want to send them out of your API, or you can start handing out VectorWrapper objects, but you have to start checking to see if incoming VectorLike objects are already instances of VectorWrapper so you don't wrap them twice! Hmm... Not so perfect, after all.

This is when you start thinking arrogantly and make the mandate, "in order to use my library, you WILL use MY library's vector class!" All this does is shift the complexity of juggling these types to your library's users. Plus, it adds yet another vector class floating around in the JavaScript run-time. No one is happy with this.

"But, wait!" Some clever programmer interjects, "you're going about this all wrong! Here, look!"

// Just make all your vector math into functions instead!  =)
function length(op: VectorLike): number { /* length stuff */ }
function add(op1: VectorLike, op2: VectorLike): VectorLike { /* add things, return op1 */ }
function scale(op: VectorLike, scalar: number): VectorLike { /* scale stuff, return op */ }

Wow, look at that! Behavior that works with VectorLike and doesn't need any additional abstraction! What this clever programmer stumbled upon is something called procedural programming. But you might be seeing another problem:

import { add, scale } from 'vector-math';

let v1 = { x: 2, y: 4 };
let v2 = { x: 6, y: 8 };
let v3 = { x: 10, y: 12 };
let acc = { x: 0, y: 0 };

// Get the average of three vectors.
let avg = scale(add(add(add(acc, v1), v2), v3), 1 / 3);

That's quite the mess! Not to mention you have to start importing these vector math procedures where ever you want to use them in your code, polluting your scopes with functions floating all over the place. Unfortunately, in this case our behavior is too far separated from the structures they're made to operate on. Its uncomfortable and unwieldy, and why we moved away from it long ago.

So, while TypeScript solves the problem of structure with interfaces, it is far from graceful when dealing with the behavior of these structures.

But wait a minute! Maybe our procedural programming friend is on to something. The first parameter of all those vector math functions all share the same structure...

Extensions are Procedural

If you have a look at the Wikipedia page on Extension Methods, you'll see a quote right at the top by the principal developer of C#'s compiler: "Extension methods certainly are not object-oriented."

And he is right, and you can see this in the way C#'s extension methods are declared. Let's do that with our vector math procedures for comparison, real quick.

public static class VectorMathExt {
  public static Double Length(this IVector op1, IVector op2) { /* length stuff */ }
  public static IVector Add(this IVector op1, IVector op2) { /* add stuff, return op1 */ }
  public static IVector Scale(this IVector op1, double scalar) { /* scale stuff, return op1 */ }
}

Now, with these extensions in scope, where ever we have an IVector object, we can call these procedural functions. As extensions, the compiler marries this behavior to any IVector making it so we can call vec.Add(...) instead of having to use VectorMathExt.Add(vec, ...). Since this is all handled for us by the compiler, we also only have to do a single import statement, and since they're namespaced into VectorMathExt, the scope pollution is minimal. But, once they are in-scope, the compiler handles the rest, replacing our nice dot-operator calls to the extension methods with the more unwieldy static class calls.

You might have noticed a while ago I keep bolding a couple of words when they're near each other: structure and behavior. That's because, deep down, this is how programs are divided up. You cannot have one without the other and still have a working program. There are many ideas on how these two concepts should inter-relate. Object-oriented programming has the structure owning the behavior: the familiar class. Functional programming has the behavior owning the structure: data lives in the stack (as much as possible).

In between, you have procedural programming, where structure and behavior do not own each other; they stay separate. Instead, structure and behavior infer each other. A procedural programmer's job is to find structures and behaviors that agree with each other and use them together, but, as we saw, this is very tedious. Now, if there were a way to map procedural functions on to compatible data structures automatically, this would be a great relief for procedural programming in JavaScript.

Basically, extensions take procedural programming and give it that object-oriented flavor we crave, and it does all this without destructively changing the underlying types. That C# IVector interface is the same as our TypeScript VectorLike interface, just an x property and a y property, with or without extension methods.

But many of you already know how extensions work. It's the fact that this is compiler magic and these extension methods cannot be laid upon the JavaScript world neatly. These extension methods vanish when they're not used in a method call! They won't act like they have these behaviors if they're passed into different scopes that lack these extensions! It would be weird!

It's Already Weird, Dude

The idea of call-site rewriting seems strange to people coming out of JavaScript, and that's largely because they're coming from an interpreted language; JavaScript doesn't have a compiler. To a JavaScript developer, there exists only 'run-time' and anything else is sorcery and witchcraft.

However, just by virtue of using TypeScript, you must deal with 'compile-time' abstractions. I would bet that everyone here stumbled on someVariable instanceof MyInterface at one point or another and thought for a moment that TypeScript's compiler had a bug in it. But you figured it out; you understand now that 'types' are a compile-time thing and 'values' are a run-time thing, and there is a division between them.

And that is what people will learn to understand with extensions. Extensions are another one of those compile-time abstractions that don't exist in the same way in run-time.

But Only if You're Ready

Keeping that in mind, it would be a terrible idea to just start piling extensions onto everything. C# made the smart choice of making extensions opt-in; in order to use them, you must bring them into scope with the using directive. Other languages which have features similar to extensions, like Rust's traits, are also opt-in and strict about it. Being opt-in, we also get the advantage that adding extensions to the compiler will not break existing code.

It should be little different for TypeScript, especially if it will take some time for programmers to adapt to the concept.

The Missing Half

TypeScript provides a means of conjuring structure to help cope with that structural type-system, the interface and type keywords. Unfortunately, it has no simple means of joining behavior to these structures. This is what extensions provide and the problem they solve in TypeScript.

Ponder the following for a moment: Interfaces describe the structure of a type. Extensions describe the behavior of a structure.

It's starting to sound a lot like extensions are the missing compliment to interfaces. Interfaces cannot have behavior; they can define space for behavior, but that isn't the same thing as having it. Where-as extensions cannot have structure, but they can define what particular structures are capable of doing. It's getting harder and harder to see how you can have one of the features but not the other.

Conclusion

As I see it, we have a feature that is only half-implemented in TypeScript. We got interfaces that poof structure into existence; where is the thing that poofs behavior into existence to go with it? It's hard to argue that extensions do not strongly compliment TypeScript's structural type-system. It would make coping with many of the challenges that exist in the domain of JavaScript so much easier.

That being said, I have been working on a complete proposal, but I think I'll wait to see if the mood changes, after everyone has digested this, before I finish it.

JHawkley avatar Nov 29 '16 03:11 JHawkley

@JHawkley please don't confuse the issue - it's not that extension methods aren't awesome and desirable. Everyone who uses C# recognizes their value. It's that they cannot[1] (cannot [2]) be added to the language in a way that actually works, for any suitable definition of "works". See https://github.com/Microsoft/TypeScript/issues/9#issuecomment-74302592

[1]: SRSLY CANNOT [2]: can't even

RyanCavanaugh avatar Nov 29 '16 17:11 RyanCavanaugh

@JHawkley If your proposal really fits in a JS world then maybe you can post it on esdiscuss or WICG so that the whole JS community can benefit from it :D

I will definitely be happy to see that happen.

saschanaz avatar Nov 29 '16 17:11 saschanaz

@RyanCavanaugh I actually kind of found your challenge course kind of funny, at least how you began it. Thing is: C# can't run that course with its extensions, and it is C# and it does have the CLR.

In C#, having an extension mapped to a type does not actually change the underlying type. If an object didn't match an interface before it had an extension, it still won't after. The compiler would helpfully inform you of the type mismatch. Extensions (at least how C# implements them) do not change types; they only associate free-floating procedures to types, making calling those procedures more convenient.

Fundamentally, your challenge course is confusing two concepts: extensions vs monkey-patches.

The core difference is one virtually extends an object in compile-time and the other ACTUALLY extends it in run-time. Your challenge course is making the assumption that extensions would need to work the same way in both domains, and if they don't, they fail and are therefore unworkable as a feature.

But extensions are not monkey-patches. There are other proposals for this kind of feature elsewhere. Extensions solve a different problem from monkey-patches, and so must be given different treatment and have different expectations.

They do work in TypeScript, just not in the same way as a monkey-patch. Also, the extensions that TypeScript would produce would also work fine in JavaScript; the popular library UnderscoreJS is an example of what a TypeScript library built on extensions would compile into. Perfectly usable! (In fact, a prime candidate for having its definition files re-written to support extensions.)

Also, as an aside: the only language I currently know of that could actually run your challenge course is Scala, with its implicits. Those are, uhh... really wild, though. ScalaJS exists and it does manage to bring the feature to JavaScript, but I am certain that the structures it compiles into are probably unsuitable for use in bare JavaScript, though.

JHawkley avatar Nov 29 '16 19:11 JHawkley

Actually, @RyanCavanaugh, I think I see what you were saying. Like a lot of people, I got caught up in the whole idea of C#-style extension methods when your suggestion clearly states "adding members to an existing type's prototype"; that's obviously a case for run-time monkey-patching. And that definitely wouldn't work, especially now that we have union and intersection types to deal with. Although compile-time abstractions were discussed thoroughly, it wasn't really what the suggestion was originally about.

But, now I'm not sure what to do. I still think compile-time abstractions for associating behavior to types does work for TypeScript and has great merit for both it and JavaScript. It feels like I should create a new issue (or find an old one) that pertains specifically to compile-time extension methods, but I am pretty certain that, by now, the issue would be immediately marked a duplicate and I'd be sent right back here. I mean, this IS issue number 9. It's an old, old discussion...

But, I've expended considerable energy on this already, and I think I laid the case for compile-time extension methods as well as I could, where it was mostly discussed. At this point, either people understand the difference and see that it works or the idea dies here. I hope it doesn't, but TypeScript will continue to be a great language all the same.

JHawkley avatar Nov 30 '16 07:11 JHawkley

Again, it would be awesome to have that feature. It's super handy. If we could wave a magic wand ✨ and make extension methods happen, we most likely would. There's no question about the desirability of the feature.

The constraint of compiling to simple JS simply makes it impossible to implement. If someone somehow came up with a way to introduce that behavior (in terms of JS emit) while still meeting our other design constraints, I think it'd be something we were really excited about. But the fact that five years later no one has come up with anything, despite everyone wanting it and wishing it existed, strongly implies that it cannot be done.

RyanCavanaugh avatar Nov 30 '16 17:11 RyanCavanaugh

I really agree with you, @RyanCavanaugh!

danfma avatar Nov 30 '16 17:11 danfma

I think something like binding+pipelining will work without the typing problems because the syntax is explicit.

saschanaz avatar Nov 30 '16 17:11 saschanaz

@RyanCavanaugh I still don't understand why it can't be simple syntactic sugar. All extensions are simply a static method/function that takes this as the first property. Why can't TSC simply rewrite the function insanity for us?

interface IEnumerable<T> {
   getEnumerator():IEnumerator<T>
}
// Extensions:
export function where<T>(this a:IEnumerable<T>, p:Predicate<T> ):IEnumerable<T>
{ /*... */ }
export function select<T,TResult>(this a:IEnumerable<T>, s:Selector<T,TResult>):IEnumerable<TResult>
{ /*... */ }
import {* as Linq} from "Linq";

function filter<T,TResult>(source:IEnumerable<T>, p:Predicate<T>, s:Selector<T>)
{
    return source.where(p).select(s);
}

... results in:

function filter(source, p, s)
{
    return select(where(source,p),s);
}

As always, I understand that there could be collisions if the existing objects had a .where or .select member. But: 1) In this case, the intention is clear because of the restrictive interface. 2) The compiler should be able to detect the collision and warn for both the cases where the extension itself is breaking the rule, or being broken when applied to the object being extended. 3) Also, the compiler should be able to identify if there is a difference in signatures between the two and even potentially create a virtual overload because of it.

electricessence avatar Nov 30 '16 19:11 electricessence