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'
Related
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
I have encountered what would seem like an extremely simple issue to solve, and I think I may be missing something.
The program crashes when attempting to create a recursive variable (?) with the typed to a class while being a member of the said class. For example:
class A {
var x: A
init() {
x = A()
}
}
Checking the crash log suggests a recursion of sorts, with init() being called infinitely.
Is there a proper method/syntax to solve this sort of problem, given that I need the variable x to be typed to class A, and the initializer must initialize variable x to class A?
It's obvious that at some step you should left property x uninitialized. So, thats better to declare it as Optional, and initialize it after instance was created:
class A {
var x: A?
}
let mainObject = A()
let nestedObject = A()
mainObject.x = nestedObject
Not sure but i think you are looking for this
class A {
var x: A?
init() {
}
anothermethod() {
x = A()
}
}
and you can call this method like
let a = A()
a.anothermethod()
I'm trying to figure out how to create a pointer to a pointer in Swift. Now, I know we don't exactly have pointers in Swift, but here is what I am trying to accomplish:
var foo = objA() //foo is variable referencing an instance of objA
var bar = foo //bar is a second variable referencing the instance above
foo = objA() //foo is now a reference to a new instance of objA, but bar
//is still a reference to the old instance
I would like to have bar be a reference to the foo variable instead of it being a reference to the foo object. That way, if foo becomes a reference to a different object, bar goes along for the ride.
One way of having a secondary reference to a variable would be a computed variable:
class C {}
var a = C()
var b: C { return a } // b is a computed variable, returning the current value of a
b === a // true
a = C()
b === a // true, whereas "var b = a" would have made this false
I believe this is what you want:
class O {
let n: Int
init(n: Int) { self.n = n }
}
var foo = O(n: 1)
var bar = withUnsafePointer(&foo) {$0}
print(bar.pointee.n) // Prints 1
foo = O(n: 2)
print(bar.pointee.n) // Prints 2
(Replace pointee with memory for Swift < 3.0)
I really don't know why you'd want that though.
You can try to use UnsafeMutablePointer. See an example below:
let string = UnsafeMutablePointer<String>.alloc(1)
string.initialize("Hello Swift")
print(string.memory)
let next = string
next.memory = "Bye Bye Swift"
print(string.memory) // prints "Bye Bye Swift"
But it smells a little and It will be better to avoid using technique like this.
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
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" }()
}