You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to @yosuke-furukawa san, something like python's __call__ method (that's for objects, and in this case for classes) will be useful if introduced.
Many nodejs modules (ex. https://github.com/flatiron/nock) are providing a kind of tricky API, that is a function object which returns new instances every time it's called. They are alternative style of class and JSX doesn't have a means of getting access to these functions.
I suggest JSX introduce a special method called __new__, which enables users to call classes directly as if it is an object.
varnock=require('nock');// creating a nock object via function call, not `new` operationvarcouchdb=nock('http://myapp.iriscouch.com');
// A class that has a `__new__` method can called as if an objectnativeclassnock{staticfunction__new__(url : string) : scope;}="require('nock')";class_Main{staticfunctionmain(args : string[]) : void{varscope=nock();}}
No objections under the premise that the feature will be limited to native classes.
Besides, I believe that the attribute should be named __call__ instead of __new__ since the role of the object (that is invoked as a function) would not be limited to creating new objects.
Fundamentally there's no difference between the semantics, only the syntaxes differ. From the aspect of extensibility I prefer __call__ (or __new__) but I guess it'd be not JSXish, rather too pythonic.
@wasabiz
What about introducing both __new__ and __call__, since they are different invocation methods.
IMO we should support definitions like below.
native class Foo {
function __new__() : Bar;
function __call__() : Bar;
// static function __new__(); // cannot be defined, since it is the same as `function constructor`
static function __call() : Bar;
}
In the above snippet, new (new Foo()), (new Foo())(), and Foo() are permitted.
Activity
yosuke-furukawa commentedon Nov 25, 2013
👍
kazuho commentedon Dec 4, 2013
No objections under the premise that the feature will be limited to
nativeclasses.Besides, I believe that the attribute should be named
__call__instead of__new__since the role of the object (that is invoked as a function) would not be limited to creating new objects.nyuichi commentedon Dec 4, 2013
@kazuho
Another idea is that use anonymous method to check this kind of attribute, see
Fundamentally there's no difference between the semantics, only the syntaxes differ. From the aspect of extensibility I prefer
__call__(or__new__) but I guess it'd be not JSXish, rather too pythonic.kazuho commentedon Dec 4, 2013
@wasabiz
What about introducing both
__new__and__call__, since they are different invocation methods.IMO we should support definitions like below.
In the above snippet,
new (new Foo()),(new Foo())(), andFoo()are permitted.