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

Proposal: Static constructors #8727

Closed
nin-jin opened this issue May 21, 2016 · 3 comments
Closed

Proposal: Static constructors #8727

nin-jin opened this issue May 21, 2016 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@nin-jin
Copy link

nin-jin commented May 21, 2016

Example:

class Component {
    static all : Array<typeof Component>
    static constructor() {
        Component.all.push( this )
    }
}

class MyButton extends {}
class MyCheckbox extends {}
class MyRadio extends {}

console.assert( Component.all[0] === MyButton , 'registration MyButton' )
console.assert( Component.all[1] === MyCheckbox , 'registration MyChackbox' )
console.assert( Component.all[2] === MyRadio , 'registration MyRadio' )

This not works. But we have workaround:

/// Support for static constructors
var __extends = ( Sub , Sup ) => {
    for( var prop in Sup ) if( Sup.hasOwnProperty( prop ) ) Sub[ prop ] = Sup[ prop ]
    Sub.prototype = Object.create( Sup.prototype )
    Sub.prototype.constructor = Sub
    if( Sub.initializer ) Sub.initializer()
};

class Component {
    static all : Array<typeof Component> = []
    static initializer() {
        Component.all.push( this )
    }
}

class MyButton extends Component {}
class MyCheckbox extends Component {}
class MyRadio extends Component {}

console.assert( Component.all[0] === MyButton , 'registration MyButton' )
console.assert( Component.all[1] === MyCheckbox , 'registration MyChackbox' )
console.assert( Component.all[2] === MyRadio , 'registration MyRadio' )

But:

  • This is hack.
  • initializer calls only on descendant classes.

We can use decorators:

class Component {
    static all : Array<typeof Component> = []
    static register( comp : typeof Component ) {
        Component.all.push( comp )
    }
}

@ Component.register
class MyButton extends Component {}

@ Component.register
class MyCheckbox extends Component {}

@ Component.register
class MyRadio extends Component {}

console.assert( Component.all[0] === MyButton , 'registration MyButton' )
console.assert( Component.all[1] === MyCheckbox , 'registration MyChackbox' )
console.assert( Component.all[2] === MyRadio , 'registration MyRadio' )

But:

  • Too more code.
  • User may forget to register component and don't understand why his code not works.
@basarat
Copy link
Contributor

basarat commented May 24, 2016

Static constructors have been asked before : #265

Where the pattern was recommended https://basarat.gitbooks.io/typescript/content/docs/tips/staticConstructor.html 🌹

@mhegazy
Copy link
Contributor

mhegazy commented May 24, 2016

indeed a duplicate of #265. closing.

@mhegazy mhegazy closed this as completed May 24, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label May 24, 2016
@nin-jin
Copy link
Author

nin-jin commented May 24, 2016

@basarat, Dependency on caring user - antipattern. Best practice - automatic enshuring of invariants. Static constructors is minimal feature for that.

@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants