TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Partial classes

Open disshishkov opened this issue 12 years ago • 224 comments

Add support of partial classes. Mixins is not the same, because it's run-time realization. Need compile realization, where partial classes will be combine into one before converting typescript to javascript.

//FileA.ts
partial class ClassA
{      
    constructor(public name: string) {}
    public sayHello(): string { return "hi!"; }
}

//FileB.ts
partial class ClassA
{
   public sayBye(): string { return "by!"; }
}

will be:

partial class ClassA
{      
    constructor(public name: string) {}
    public sayHello(): string { return "hi!"; }
    public sayBye(): string { return "by!"; }
}

disshishkov avatar Aug 29 '14 08:08 disshishkov

Inheritance is messier and has more overhead.

On Mon, Aug 10, 2015, 10:09 AM Gorgi Kosev [email protected] wrote:

Why not use inheritance for code generation?

— Reply to this email directly or view it on GitHub https://github.com/Microsoft/TypeScript/issues/563#issuecomment-129468291 .

JoshMcCullough avatar Aug 10 '15 14:08 JoshMcCullough

​Inheritance creates useless name couplings

yahiko00 avatar Aug 10 '15 14:08 yahiko00

Inheritance is less awesome because it:

  1. Deepens the prototype chain needlessly and slows down instantiation, although thats not a big deal for most apps, likely everything but games;
  2. For lack of abstract and override either loses some type safety or introduces the need for some ackward patterns like casting between the two types;
  3. Is confusing overall as having a base class communicates the idea that it can be extended by other classes, which is rarely the case with codegen

Not saying typescript HAS to have partial classes to support codegen, only that partial classes are so much better at it than inheritance or composition.

hdachev avatar Aug 10 '15 14:08 hdachev

@JoshMcCullough Inheritance overhead in JS is minimal, and I don't see how its messier than partial classes: with those you can potentially split a class into 10 files and chase your methods all over the place.

@yahiko00 What kind of "useless name coupling" are we talking about here? Do you refer to the things you'd need to do to get No. 2 below (hardcoding the name of the extended class)?

@hdachev

  1. There is some overhead, but its quite minimal - only noticeable if you're doing things that translate directly to a couple of machine instructions
  2. Good point - you don't get the extended class when you have to reference the generated class from other generated classes.
  3. Those classes are indeed meant to be extended - otherwise you could generate them and be done with it, right?

AFAIK this is not on the ES7+ radar, is it? That might be problematic...

In any case, attaching extra things to the prototype of an existing class is quite trivial in JavaScript and not at all uncommon, so it might be a good idea to have syntax for that...

spion avatar Aug 10 '15 15:08 spion

It's straightforward in TS but the JS behind the scenes would double the "protoyping" if you were using base classes for all of your generated data objects. Probably not a serious performance impact unless you have a ton of data objects, but it still is unnecessary when we know that "partial classes" are possible.

On Mon, Aug 10, 2015 at 11:03 AM Gorgi Kosev [email protected] wrote:

@JoshMcCullough https://github.com/JoshMcCullough Inheritance overhead in JS is minimal, and I don't see how its messier than partial classes: with those you can potentially split a class into 10 files and chase your methods all over the place.

@yahiko00 https://github.com/yahiko00 What kind of "useless name coupling" are we talking about here? Do you refer to the things you'd need to do to get No. 2 below (hardcode the name of the extended class)?

class MyClass extends GeneratedMyClass { ... }

@hdachev https://github.com/hdachev

There is some overhead, but its quite minimal https://jsperf.com/prototype-chain-vs-direct-calls - only noticeable if you're doing things that translate directly to a couple of machine instructions 2.

Good point - you don't get the extended class when you have to reference the generated class from other generated classes. 3.

Those classes are indeed meant to be extended - otherwise you could generate them and be done with it, right?

AFAIK this is not on the ES7+ radar, is it?

In any case, attaching extra things to the prototype of an existing class is quite trivial in JavaScript and not at all uncommon, so it might be a good idea to have syntax for that...

— Reply to this email directly or view it on GitHub https://github.com/Microsoft/TypeScript/issues/563#issuecomment-129483174 .

JoshMcCullough avatar Aug 10 '15 15:08 JoshMcCullough

@Davidhanson90 that file has zero classes in it. Did you mean to post this in a different thread?

RyanCavanaugh avatar Aug 18 '15 20:08 RyanCavanaugh

I'll post onto the correct thread. https://github.com/Microsoft/TypeScript/issues/447

Davidhanson90 avatar Aug 18 '15 20:08 Davidhanson90

+1

kirilpopov avatar Sep 29 '15 11:09 kirilpopov

+1

do34 avatar Oct 14 '15 13:10 do34

+100

Mike-Loffland avatar Nov 05 '15 22:11 Mike-Loffland

+1

LeandroBarral avatar Nov 16 '15 17:11 LeandroBarral

+1

iKBAHT avatar Nov 17 '15 11:11 iKBAHT

+1 for code generation. I have been getting into angularjs and webapi and I want to make a tool that essentially creates automatic JS definitions from my c# objects and services. I want to then be able to extend these classes without having to edit the "scaffolded" JS definitions. I've see that a few other people are requesting this and it seems like we have similar use cases.

TonyValenti avatar Nov 27 '15 14:11 TonyValenti

+1 for code generation

s-KaiNet avatar Nov 27 '15 14:11 s-KaiNet

+1 for code generation, angular has lots of boilerplate code, we're already doing lots of generation of it, but with partial classes we would be able to do much more.

dziedrius avatar Dec 03 '15 13:12 dziedrius

+1 yet again for code generation. Trying to extend classes in another file is messy at best.

GrantErickson avatar Dec 03 '15 17:12 GrantErickson

@RyanCavanaugh, what is needed to get this out of "+1" mode and moving in a productive direction?

RandScullard avatar Dec 03 '15 17:12 RandScullard

-1 a following syntax is better for mixins: #2919

falsandtru avatar Dec 06 '15 15:12 falsandtru

Not sure why can't we have both partial classes and the mixins; two totally unrelated features.

ghost avatar Dec 06 '15 21:12 ghost

+1

This would be nice when code is generated, but you don't want to modify it to add things to the generated code.

Vaccano avatar Dec 08 '15 03:12 Vaccano