TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Feature Request / Proposal: Traits

Open yasselavila opened this issue 12 years ago • 31 comments

ORIGINALLY PROPOSED IN http://typescript.codeplex.com/workitem/838

Traits, as "compile-time" partial classes, would perfectly fit with TS ideology and resolve problems of multiple inheritance/mixins.

Traits in Scala: http://en.wikibooks.org/wiki/Scala/Traits Traits in PHP: http://php.net/trait

I propose minimal traits similar to PHP implementation.

trait FooTrait {
   // same as class definition, but no constructor allowed, e.g.:
   public foo() {
       return "foo";
   }
   private bar() {
       return "bar";
   }
}

class Foo {
    import FooTrait; //not insisting on exact keyword and syntax, just smth to start with

    // ...
}

class Bar {
    // rename methods:
    import FooTrait(foo => tfoo, bar => tbar);

    // and to include another trait, there is another import line:
    // import BarTrait;

    // ...
}

The code above could be compiled to JS below (can be optimized, just showing the main idea):

var FooTrait = (function () {
    function FooTrait() {
        throw "Cannot instantiate trait FooTrait";
    }
    FooTrait.prototype.foo = function () {
        return "foo";
    };
    FooTrait.prototype.bar = function () {
        return "bar";
    };
    return FooTrait;
})();

var Foo = (function (_super, FooTrait) {
    function Foo() { }
    Foo.prototype.foo = FooTrait.prototype.foo;
    Foo.prototype.bar = FooTrait.prototype.bar;
    return Foo;
})(undefined, FooTrait);

var Bar = (function (_super, FooTrait) {
    function Bar() { }
    Bar.prototype.tfoo = FooTrait.prototype.foo;
    Bar.prototype.tbar = FooTrait.prototype.bar;
    return Bar;
})(undefined, FooTrait);

Unresolved name conflicts should raise a compile time error.

I think it would be a great advance for the language. But the proposal has more than one year made (in codeplex) ​​and has not yet been implemented.

yasselavila avatar Jul 30 '14 15:07 yasselavila

@jbondc

As long as the composing class implements all conflicting methods, there is no conflict reported by the compiler:

To be able to do this, programmer must know about all methods in all imported mixins and which of them are in conflict. Why he should if it is possible resolve this automatically like dojo/declare does (like in dstore example by @csnover)?

anatoliyarkhipov avatar Apr 10 '15 12:04 anatoliyarkhipov

There's a lot of overlap here with mix-ins; it would be bad (or at least treacherous) to do both. With mix-ins being a potential part of ES7/ES8+, we want to take the runtime side of this very conservatively and get TC39 to agree on a proposal in this area first.

Features specific to the type system to support these patterns are still on the table, but we'll track those in other issues.

RyanCavanaugh avatar Apr 28 '15 00:04 RyanCavanaugh

Is there an update on how/if mixins will become a first class capability of typescript rather than a bit of a hack with separated registration as it currently stands?

zakhenry avatar Jan 22 '16 11:01 zakhenry

Is there an update on how/if mixins will become a first class capability of typescript rather than a bit of a hack with separated registration as it currently stands?

I am sure I will be corrected if I am wrong, but the TypeScript team have made it clear that because this is a domain that TC39 has indicated they will address, the team feel that this is something it doesn't feel it can introduce as it would likely cause incompatibilities with ES in the future.

The implications are, those of us who are passionate about it should be championing it within TC39. The concepts of mixins/traits were part of the originally Harmony proposals, but didn't make it into the spec. Also, as far as I am aware, there isn't any active advocacy on this specific area in TC39 at the moment. Once there is a proposal sufficiently through the process in TC39, then I believe the TypeScript team would be more than glad to implement it.

kitsonk avatar Jan 22 '16 11:01 kitsonk

@kitsonk thanks for the update, I guess we just have to hope for TC39, I'll see if I can find any discussion on the matter. I feel at this point that traits are the only major capability missing - I'm currently managing a site with ~50k sloc TS, and the mixin registrations are somewhat unwieldy.

zakhenry avatar Jan 22 '16 11:01 zakhenry

+1 for this feature

sanex3339 avatar Jan 24 '17 16:01 sanex3339

What's the status on this feature?

ackimwilliams avatar Feb 21 '17 07:02 ackimwilliams

What's the status on this feature?

See https://blogs.msdn.microsoft.com/typescript/2017/02/02/announcing-typescript-2-2-rc/ ... basically they're not adding new language grammar, but by making some changes to the way Classes work, it's possible/easier to achieve now.

dylans avatar Feb 22 '17 13:02 dylans

One powerful pattern that is possible with traits is this (similar to what @jbman mentioned):

trait AccountService {
  debit: (account: Account, amount: number) => Account;
  credit: (account: Account, amount: number) => Account;
  transfer(from: Account, to: Account, amount: number): [Account, Account, number] {
    return [this.debit(from, amount), this.credit(to, amount), amount]  
  }
}

This is not something that TC39 can help us with as in this particular form it's only relevant to statically typed languages. It can be done with abstract classes but then we lose the benefit of the mixin pattern as we move the problem into the inheritance area. Trait composition also makes pattern such as "cake" pattern possible.

alexeygolev avatar Mar 07 '17 07:03 alexeygolev

+1

samfrances avatar Jul 28 '17 11:07 samfrances

You can have a look at a simple library i wrote at TypeScript Mix I think it fits nicely into how you would want to use traits/mixins

michaelolof avatar Nov 04 '17 11:11 michaelolof