CoffeeScript: Accessing outer class objects from inner class instance - coffeescript

If there any way to access outer class fields from inner class instance, EXCEPT passing outer class instance to inner class constructor?
To be more specific, I have a simple example:
class Test
constructor: (#number) ->
class SubTest
constructor: (#name) ->
toString: () ->
console.log #name, #number
getSubTest: () ->
return new SubTest "SubTest"
test = new Test 10
test.getSubTest().toString() # SubTest undefined
So, I want to get "SubTest 10" instead of "SubTest undefined". Is it possible?

Good news! It turns out if you create the closure over # yourself, it works just fine:
class Test
self = []
constructor: (#number) ->
self = #
class SubTest
constructor: (#name) ->
toString: () ->
#name + self.number
getSubTest: () ->
return new SubTest "SubTest"
test = new Test 10
v = test.getSubTest().toString()
alert v
Translates to :
var Test, test, v;
Test = (function() {
var SubTest, self;
self = [];
function Test(number) {
this.number = number;
self = this;
}
SubTest = (function() {
function SubTest(name) {
this.name = name;
}
SubTest.prototype.toString = function() {
return this.name + self.number;
};
return SubTest;
})();
Test.prototype.getSubTest = function() {
return new SubTest("SubTest");
};
return Test;
})();
test = new Test(10);
v = test.getSubTest().toString();
alert(v);
Output:
SubTest10

It's an old question but the accepted answer doesn't work if multiple instances of the outer class are needed (as pointed out by #costa-shapiro). Here is an alternative approach to create the closure with the inner class.
SubTest = (test) ->
class SubTest
constructor: (#name) ->
toString: () =>
console.log #name, test.number
class Test
constructor: (#number) ->
#SubTest = SubTest #
getSubTest: () =>
return new #SubTest "SubTest"
test = new Test 10
test.getSubTest().toString()

Related

Set a non-scalar variable to null and return whatever it's referencing in a single statement?

In a project I'm working on I have the following kind of operation in more than a few places:
MyClass obj = editObj;
editObj = null;
doSomething(obj);
The point of this is to set the variable editObj to reference null, while "transferring" whatever it's referencing to another variable so I can keep using it.
To put it into context, here's a very simplified program:
class MyClass{
String text = '';
MyClass(this.text);
#override
String toString() => 'MyClass{text: $text}';
}
MyClass editObj;
void doSomething(MyClass obj) {
print('1 obj: $obj editObj: $editObj');
Future.delayed(Duration(seconds: 1), (){
obj.text = 'two';
print('2 obj: $obj editObj: $editObj');
});
}
void main() {
editObj = MyClass('one');
MyClass tmp = editObj;
editObj = null;
doSomething(tmp);
}
This prints
1 obj: MyClass{text: one} editObj: null
2 obj: MyClass{text: two} editObj: null
If main() instead looked like this
void main() {
editObj = MyClass('one');
doSomething(editObj);
editObj = null;
}
the results would be
1 obj: MyClass{text: one} editObj: MyClass{text: one}
2 obj: MyClass{text: two} editObj: null
Of course, I could simply set editObj to null at the start of doSomething(), but I'd rather handle it from the calling code.
Is there a less verbose way of achieving this? I'm thinking something like
doSomething(editObj = null) // Doesn't work
i.e. setting editObj (the variable!) to null while at the same time returning whatever it's referencing.

TypeScript class decorator that modifies object instance

I'm making a plugin for Aurelia and need a class decorator that
adds attributes to the new object instance, and
calls an external function with the new object as an argument.
I've looked through examples, and so far I've put together ("pseudo-ish" code)
return function addAndCall(target: any): any {
var original = target;
var newConstructor = function (...args) {
original.apply(this, args);
this.newAttribute = "object instance value";
ExternalModule.externalFunction(this);
};
newConstructor.prototype = Object.create(original.prototype);
newConstructor.prototype.constructor = original;
return <any>newConstructor;
}
but
I'm not entirely clear on the details here (or what is actually needed), and
it might not work properly since I'm getting Aurelia errors when using objects instantiated from classes with this decorator (and I suspect it's my decorator rather than the Aurelia framework that's buggy).
Any help and explanation would be greatly appreciated!
Why not just assign those properties to the prototype, and subsequently assign to the instance on first invocation
// decorator
function addAndCall(cb: Function, newField: string) {
// cb is now available in the decorator
return function(ctor: Function): void {
Object.defineProperty(ctor.prototype, newField, {
value: function(...args: any[]) {
return Object.defineProperty(this, newField, {
value: function(...args: any[]) {
console.log(newField, ...args);
}
})[newField](...args);
}
});
cb(ctor);
}
}
let callMe = (decoratedCtor) => console.log(decoratedCtor);
#addAndCall(callMe, 'propertyName')
class AddToMe {}
let addToMe = new AddToMe();
(<any>addToMe).propertyName(1, 2);
Here's a working version:
function addAndCall(target: any) {
var original = target;
function construct(constructor, args) {
var c: any = function () {
this.newAttribute = "object instance value";
ExternalModule.externalFunction(this);
return constructor.apply(this, args);;
}
c.prototype = constructor.prototype;
return new c();
}
var f: any = function (...args) {
return construct(original, args);
}
f.prototype = original.prototype;
return f;
}
(code in playground)

TYpescript : Static methods on Function as class

I have a fn that inherit an existing fn ( take Angular1 $q for example )
//$q original behavior
var defer = $q.defer();
defer.promise.then(function(result){})
//or
$q( (resolve, reject) => {
//promise execution here
}).then(function(result){});
If I want to decorate it, I would do :
var Qdecorator = function($delegate) {
var Q = function(resolver:any): any {
//do some extra stuff here
return $delegate.apply($delegate, arguments);
}
//Assign the static methods here:
Q.defer = function() {
//do some stuff
return $delegate.defer.apply($delegate, []);
}
//same goes for race, when, resole reject and so on
return Q;
}
Problem is that typescript complains about
Property defer, race, when, resolve, etc... does not exist on type '(resolver: any) => any'
I tried to use the IQService, and IPromise with no luck, btu I'd like to raise a more global question :
How do I define late static methods on function() that return an object without using new
I am copying pasting the answer to my question from this link:
https://www.typescriptlang.org/docs/handbook/interfaces.html
interface Counter {
(start: number): string;
interval: number;
reset(): void;
}
function getCounter(): Counter {
let counter = <Counter>function (start: number) { };
counter.interval = 123;
counter.reset = function () { };
return counter;
}
let c = getCounter();
c(10);
c.reset();
c.interval = 5.0;

How to declare instantiation in Haxe macro function

I want to create a macro that generates this code for me:
if (myEntity.get(Attack) == null) myEntity.add(new Attack());
if (myEntity.get(Confused) == null) myEntity.add(new Confused());
if (myEntity.get(Defend) == null) myEntity.add(new Defend());
if (myEntity.get(Offense) == null) myEntity.add(new Offense());
In code I'd like to declare/use it like this:
EntityMacroUtils.addComponents(myEntity, Attack, Confused, Defend, Offense);
The current macro function looks like this:
macro public static function addComponents(entity:ExprOf<Entity>, components:Array<ExprOf<Class<Component>>>):Expr
{
var exprs:Array<Expr> = [];
for (componentClass in components)
{
var instance = macro $e { new $componentClass() }; // problem is here
var expr = macro if ($entity.get($componentClass) == null) $entity.add(instance);
exprs.push(expr);
}
return macro $b{ exprs };
}
This macro function is incorrect, I get the error:
EntityMacroUtils.hx:17: characters 22-43 : Type not found : $componentClass
The problem is I don't know how to define new $componentClass(). How would I solve this?
I also want to avoid to have Type.createInstance in the output code.
One way to programmatically generate instantiation code is by using "old school" enums AST building (compatible Haxe 3.0.1+):
// new pack.age.TheClass()
return {
expr:ENew({name:"TheClass", pack:["pack", "age"], params:[]}, []),
pos:Context.currentPos()
};
An improved syntax using reification is possible:
// new pack.age.TheClass()
var typePath = { name:"TheClass", pack:["pack", "age"], params:[] };
return macro new $typePath();
Now, for a convenient "instantiation helper" function syntax, we need to do some contorsions to extract a type path from the expression we receive in the macro function:
// new Foo(), new pack.Bar(), new pack.age.Baz()
instantiate(Foo, pack.Bar, pack.age.Baz);
macro static function instantiate(list:Array<Expr>)
{
var news = [for (what in list) {
var tp = makeTypePath(what);
macro new $tp();
}];
return macro $b{news};
}
#if macro
static function makeTypePath(of:Expr, ?path:Array<String>):TypePath
{
switch (of.expr)
{
case EConst(CIdent(name)):
if (path != null) {
path.unshift(name);
name = path.pop();
}
else path = [];
return { name:name, pack:path, params:[] };
case EField(e, field):
if (path == null) path = [field];
else path.unshift(field);
return makeTypePath(e, path);
default:
throw "nope";
}
}
#end
In case anyone is in need for answers, I got this Thanks to ousado on the Haxe IRC chat:
If you do it in macro alone you can do this:
var ct = macro : pack.age.SomeTypename;
var tp = switch ct { case TPath(tp):tp; case _: throw "nope"; }
var expr = macro new $tp();
..or, if you explicitly construct tp:
var tp = {sub:'SomeTypeName',params:[],pack:['pack','age'],name:"SomeModuleName"}
As you can see, the complex type path is explicitly given here.
Unfortunately, Haxe don't really have a concise syntax for types in expression positions. You can pass ( _ : TypeName ) to provide an expression that contains a ComplexType.
But if you want to pass a type as argument, you could do it like this:
import haxe.macro.Expr;
using haxe.macro.Tools;
class Thing {
public function new(){}
}
class OtherThing {
public function new(){}
}
class TMacroNew {
macro static function instances( arr:Array<Expr> ) {
var news = [for (e in arr) {
var ct = switch e.expr { case EParenthesis({expr:ECheckType(_,ct)}):ct; case _: throw "nope"; };
var tp = switch ct { case TPath(tp):tp; case _: throw "nope"; };
macro new $tp();
}];
trace( (macro $b{news}).toString());
return macro $b{news};
}
static function main(){
instances( (_:Thing), (_:Thing), (_:OtherThing) );
}
}
..if you want a list of types, you might want to go for a parameter list like ( _ : L< One,Two,Three> ).
The accepted answer is problematic because it breaks when type parameters are involved, or when support for non-nominal types should be included.
I updated the example using two alternatives for a more concise notation for the list of types, while still allowing syntax for actual types.
import haxe.macro.Expr;
using haxe.macro.Tools;
class Thing {
public function new(){}
}
class OtherThing {
public function new(){}
}
class TPThing<T>{
public function new(){}
}
class TMacroNew {
macro static function instances( e:Expr ) {
var tps = switch e.expr {
case EParenthesis({expr:ECheckType(_,TPath({params:tps}))}):tps;
case ENew({params:tps},_):tps;
case _: throw "not supported";
}
var type_paths = [ for (tp in tps) switch tp {
case TPType(TPath(tp)):tp;
case _: throw "not supported";
}];
var news = [for (tp in type_paths) macro new $tp()];
trace( (macro $b{news}).toString());
return macro $b{news};
}
static function main(){
instances( (_:L<Thing,Thing,OtherThing,TPThing<Int>> ) );
instances( new L<Thing,Thing,OtherThing,TPThing<Int>>() );
}
}
Edit:
The L in L< ... > could be any valid type name. Its only purpose is allowing to write a comma-separated list of types in valid syntax. Since macro functions take expressions as arguments, we have to use an expression that allows/requires a type, like: ( _ :T ), new T(), var v:T, function(_:T):T {}.

in Dart, how do you use `class` when stored in a variable?

in Dart, you can assign a class to a variable, eg,
var klass = String;
but once assigned, how do you used it?
var str = new klass(); ===> ERROR: malformed type used
once one has a class in a variable, how does one use it to instantiate a new object of that class?
You have to use dart:mirrors.
import 'dart:mirrors';
class A {
A();
A.named(String value);
}
main() {
final t = A;
// same as 'new A()'
final a1 = reflectClass(t).newInstance(const Symbol(''), []).reflectee;
// same as 'new A.named('test')'
final a2 = reflectClass(t).newInstance(#named, ['test']).reflectee;
}