Why is coffeescript prepending "1" for the constructor argument - coffeescript

class MyController
constructor: (#foo) ->
translates to (this is good)
var MyController;
MyController = (function() {
function MyController(foo) {
this.foo = foo;
}
return MyController;
})();
But the following code
class MyController
constructor: (#foo) ->
#bar = foo
translates to
var MyController;
MyController = (function() {
function MyController(foo1) { //foo1 ??
this.foo = foo1;
this.bar = foo; //Now due to this, the compiler throws up!
}
return MyController;
})();
While I was expecting the following conversion
var MyController;
MyController = (function() {
function MyController(foo) {
this.foo = foo;
this.bar = foo;
}
return MyController;
})();
Is this a bug in compilation or am I missing something?
Here's the link

There is nothing wrong with CS compilator here, the only problem is that your second example is incorrect.
Let's have a closer look at your code:
class MyController
constructor: (#foo) ->
#bar = foo
First, you're assigning #foo to the first argument, which is fine.
But then you're assigning #bar to some global foo variable, which is not defined anywhere in your code.
CS renamed foo to foo1 to avoid naming collision with global foo variable.
Probably you wanted something like this instead:
class MyController
constructor: (#foo) ->
#bar = #foo
Update:
(#foo) -> is a syntactic sugar for assigning first argument value to this.foo. Naturally, CS uses temp variable to carry this assignment, though the only thing guaranteed about the name of this temp variable is that it won't collide with any of your own variables.
As for your first example, the following code should explain why CS behaves correctly there:
foo = 42
class MyController
constructor: (#foo) ->
#bar = foo

Related

How can I call super inside constructor body?

class Foo {
Foo(int y);
}
class Bar extends Foo {
int value;
Bar(int x) { // error in this line
value = x;
print("Hi there");
super(x); // error in this line
}
}
How can I call super inside constructor body?
Note:
I know I can use initialiser list to solve it but I would like to know how to call super inside method body?
Bar(int x): value = x, super(x); // works but I am not looking for it.
Dart does not support inheriting the constructors as explicitly callable methods. The initializer list you mentioned is the supported way to call unnamed super constructors in Dart.
However, you can achieve what you want with the help of named constructors. Take a look at the below example -
class Foo {
int superValue;
Foo(); //A default zero-argument constructor
Foo._init(this.superValue); //Named constructor
void initValue(int x) => Foo._init(x);
}
class Bar extends Foo {
int value;
Bar(int x) {
value = x;
print("Hi there");
super.initValue(x);
}
}
void main() {
Foo foo = Bar(10); //prints 'Hi there'
}
Hope it helps!
UPDATE
You can also call the super constructor and add other statements to the child constructor using this way -
class Foo {
int superValue;
Foo(this.superValue);
}
class Bar extends Foo {
int value;
Bar(int x) : super(x) {
value = x;
print("Hi there");
}
}
void main() {
Foo foo = Bar(10);
}

Scala: Declare a variable for holding a function (lambda) without immediate assignment

I want to declare a var that can hold a function, but I want to assign the function only later.
How do I declare such a var, e.g. when it takes no parameters and returns nothing?
I tried this but that's not accepted:
private var myFunction : ()
Later in the code, I'd assign code to it like this, I imagine:
myFunction = () => { doSomething() }
Like so:
def doSomething(): Unit = ???
class A {
private var m1: Function0[Unit] = _
// later down the line
m1 = () => { doSomething() }
private var m2: () => Unit = _
// later down the line
m2 = () => { doSomething() }
}
Just declare the type of the function using function literal syntax, or with one of the FunctionN traits.
Edit
Your mistake was declaring myFunction with invalid syntax.
Syntax is [visibility] {var|val|lazy val} {variable-name}[:<variable-type>}[=<value]
Where [] denotes optional parts and {} mandatory parts.
Visibility is assumed public if you omit it. As to the underscore you can check its meaning in this SO post

Stored properties in Swift can't refer to each other?

What is the reason I can't give a value to a stored property that depends on the value of another one in Swift 2.0?
The code below gives an error saying:
Something.Type does not have a member named 'foo'
class Something {
let foo = "bar"
let baz = "\(foo) baz"
}
This is odd, as Something.Type certainly does have a member called foo.
Is there a way around this?
Looks like you're trying to initialise the variable baz, before swift has had a chance to know that foo is a property of Something. Place your initialisation inside the init constructor.
class Something {
let foo: String
let baz: String
init () {
foo = "bar"
baz = "\(foo) baz"
}
}
You can also use lazy initialization but now you have to make it a variable:
class Something {
let foo = "bar"
lazy var baz = { "\(self.foo) baz" }()
}

Access instance constant in lazy constructor

Consider this example
class Foo {
private let bar = "bar"
lazy var baz : String = {
return "baz \(bar)"
}()
}
Unfortunately this won't compile and give the following error
'Foo.Type' does not have a member named 'bar'
I really do not want to declare bar outside the class (globally). Is there no other way to keep this inside the class and why isn't bar accessible in the first place?
TL;DR: preface with self
Swift can be quite misleading with error messages, but in this case, the answer can be deduced from the message. It is looking for bar on type Foo.Type, whereas you are trying to reference an instance variable. Here is code that works:
class Foo {
private let bar = "bar"
lazy var baz : String = {
return "baz \(self.bar)"
}()
}
In lazy props you need to say self
lazy var baz : String = {
let bar = self.bar
return "baz \(bar)"
}()

Coffeescript classes and variables

Say I have a Coffeescript class like this:
class Foo
aVar = 'foo'
someFunction = ->
anotherVar = 'bar'
Is there a way to set anotherVar as a class variable without having to declare it as null, like so:
class Foo
aVar = 'foo'
anotherVar = null
someFunction = ->
anotherVar = 'bar'
No, you can't. Let us look at a simple class:
class C
cv = null
m: -> cv
That is converted to this JavaScript:
var C = (function() {
var cv;
function C() {}
cv = null;
C.prototype.m = function() {
return cv;
};
return C;
})();
You'll notice that the "private class variable" cv is just a local variable inside the self-executing function that builds C. So, if we wanted to add a new "private class variable" to C, we'd have to open that anonymous function's scope again and add new variables. But there's no way to travel back in time and alter the scope of a function that has already executed so you're out of luck.
You don't have to define your anotherVar as null when you define it but you have to initialize it to something.
Have you ever heard about this keyword? :) CoffeeScript maps # into this:
class Foo
aVar = 'foo'
someFunction: ->
#anotherVar = 'bar'