Getting the name of a coffeescript class? - coffeescript

Will the following alert "Foo" in all browsers, even when minified?
class Foo
alert(Foo.name)
Nothing is stated in the doc, I know that IE has problems with function names, and I'm confused with the many issues opened about this, like any of these issues !

From within any method of class Foo that is included in Foo.prototype, you can insert the line
console.log #constructor.name
and it will write
Foo
to your console log. HTH.

That may depend on which version of the CoffeeScript compiler you're using. In the lastest stable release (1.3.3), a "name" property isn't generated by default.
class Foo
compiles into
var Foo;
Foo = (function() {
function Foo() {}
return Foo;
})();
Since the name property is non-standard and currently not supported by the IE, you cannot really rely on it cross-browser. Detailed information about this are available at the MDN: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Name

Related

How to get application "namespace" in Swift implicitly?

Short version:
Assuming I have an app called MyApp. In some situation I have a duplicate class name so that one is MyClass where the other is MyApp.MyClass. How can I call MyApp.MyClass having MyApp constant (do not need to change the code if I change the app name).
Why and what happened (long version):
So assume we are in this MyApp application having following code (the real one is way way longer and used allover the app):
class MyClass {
}
class SomeOtherClass {
class MyClass {
}
func findMyClass(fromMyClass: MyApp.MyClass) -> MyClass {
...
}
}
Because there are two classes called MyClass in inner scope we need to call MyApp.MyClass to get the outer one which is all good.
But it seems a bit silly when MyApp changes which is my case.
Why did it change? I actually added a new target by duplicating the original one because I wanted to have some extra and some different settings (pretty standard procedure). So I renamed the new one to have MyApp-dev where I then got an error Use of undeclared type 'MyApp'.
So is there a keyword that can replace MyApp?
My current solution is to define a typealias on file scope that is then used inside the class to replace MyApp. The problem is that the method is not private so the typealias may not be private either which means I just have an extra name defined globally in the project.
EDIT:
Also there is a setting in Xcode build settings called Product Module Name which may be overridden to explicit naming which fixes the issue. Still this not really a good general solution; I can see cases where you might reuse the same code in multiple modules (creating libraries) which has the same issue. This would still meant that code would need to be fixed for each of these modules.

Better way to call a private method

I'm using CoffeeScript to create a class and build a private method, but my code feels kludgy.
As in the example below, first I define the method with = and then I am forced to use the call method on the portion to be used. But this seems like a kludgy workaround, so I want to know if there is a cleaner solution.
class Human
constructor: (#name, #height, #weight) ->
_realWeight = ->
#weight
answerWeight: ->
console.log(_realWeight.call(#) - 5)
$ ->
ken = new Human('Ken', 165, 70)
ken.answerWeight()
TL;DR
No.
Longer Answer
There is only one way to have truly private data in javascript/coffeescript: closures.
First, lets consider some alternatives:
Symbols
Because symbols are unique they can be used to create psuedo-private data:
you can only access the property if you have a reference to the symbol its keyed to:
foo = Symbol('I am unique')
bar = {}
bar[foo] = "I am almost private"
Code that doesn't have access to foo can't easily get to that property of bar except for Object.getOwnPropertySymbols. So not easy to break, but breakable.
Underscores
Typical naming convention says that properties/methods prefixed or followed by an underscore are 'private', they are not to be used by an external caller. However, that 'privacy' is not in any way enforced by the runtime.
So lets talk about closures.
Simple Closure example
makeFoo = (something) -> getSomething: -> something
foo = makeFoo(3)
foo.something # undefined
foo.getSomething() # 3
Now there is no way to get at the parameter passed to the constructor except to call the method. This pattern, while slightly more elegant in coffeescript, is still kinda lame. Lots of duplicated function objects. Not so bad for just getSomething, but add a bunch of methods and it gets ugly fast. Also, typically not as easily optimized by the JIT compiler as foo = new Foo() would be. Fortunately, ES 2015 to the rescue:
Advanced Closure Example
Foo = null
do ->
privateData = new WeakMap()
getSomething = -> privateData.get(this)
Foo = class Foo
constructor: (something) -> privateData.set(this, something)
getSomething: getSomething
foo = new Foo(3)
foo.something # undefined
foo.getSomething() # 3
new Foo(42).getSomething() # 42
foo instanceof Foo # true
Now all instances of Foo share one copy of getSomething rather than each getting their own. The weakmap is hidden in the closure created by the IIFE, and because of the 'weak' part of WeakMap when the instance gets garbage collected the private data will be as well. You are also now potentially able to enjoy the benefits of the compiler optimizing newly created objects. Last but not least, instanceof still works properly (to the extent that it ever works properly).
Further reading.
Even More reading
Note
WeakMaps are not supported in all browsers (for IE its 11 or bust). There is a shim, but it cannot be completely polyfilled. Whether or not the shim gets close enough is a call you'll have to make.

Scala parameters for access modifiers?

What is the difference between
class Test {
private[this] val foo = 0
}
vs
class Test {
private val foo = 0
}
What all can go inside the []? Also, what should I search for when I want to look up the specs of this? I tried Googling various combinations of "scala access modifier arguments/parametrized scala access modifier" and nothing came up.
what should I search for when I want to look up the specs of this?
In The Scala Language Specification it is defined as "access modifier" and "access qualifier" (see BNF in §5.2).
What is the difference between
...
What all can go inside the []?
You can put class name, package name or this there. Here is a relevant quote from language specs that explains this (see §5.2 for more details):
The modifier can be qualified with an identifier C (e.g. private[C ]) that must
denote a class or package enclosing the definition. Members labeled with
such a modifier are accessible respectively only from code inside the package
C or only from code inside the class C and its companion module (§5.4).
An different form of qualification is private[this]. A member M marked
with this modifier is called object-protected; it can be accessed only from
within the object in which it is defined. That is, a selection p.M is only legal if the prefix is this or O.this, for some class O enclosing the reference. In
addition, the restrictions for unqualified private apply.
The first one is private for instance class, second is for class. If you use second version you have access from another instance of Test class (it's usefull for equals method or similiar).

What is the meaning of private[context]

I'm trying to wrap my head around the Scala language and figured the best way to learn is to put it into practice. When copy pasting code between a Java project (Spring) and my Scala project the IDE did a conversion I do not understand. Searching for it on the internet and in the docs gave me nothing to work with unfortunately.
The code:
#Bean private[context] def passwordEncoder: PasswordEncoder = {
return new BCryptPasswordEncoder
}
When compiling the above code the compiler complains:
`error: context is not an enclosing class`
Can anybody explain what the private[context] part means?
context is just a placeholder, where you can fill in the context in which you'd like the method to be private. This is optional though. If you don't specify the context, the member becomes "class-private", which afaik behaves like private does in Java.
Background: Scala offers more than one degree of access specification: the object-private specification, i.e. private[this], stipulates that the member in question can only be seen by members called on that same object, not from different objects, even if they are of the same type. Instead of this you can also use a package name or even root, which is an alias for the root namespace.
More information on this can be found in "Section 5.2 - Modifiers" of the Scala Language Reference:
The private modifier can be used with any definition or declaration in a template.
[...]
The modifier can be qualified with an identifier C (e.g. private[C]) that must denote a class or package enclosing the definition. Members labeled with such a modifier are accessible respectively only from code inside the package C or only from code inside the class C and its companion module (§5.4).

Global classes with Meteor >0.6.0 and CoffeeScript

Since the release of Meteor 0.6.0 and the addition of file-level JavaScript variable scoping, I'm facing an issue using CoffeeScript classes, each of them being defined in its own respective file.
foo.coffee:
class Foo
...
subfoo.coffee:
class Subfoo extends Foo
...
As expected, and because of the changes introduced in Meteor 0.6.0, I'm getting the following error:
ReferenceError: Foo is not defined
Here's my question: how should one handle class definitions across files with CoffeeScript and Meteor >0.6.0? Ideally: is there a convenient way not to modify too much the way classes are defined in order to make sure these definitions (and core parts of my application) are not Meteor-dependent?
As noted in the CoffeeScript section of the docs:
Global variables can be set in CoffeeScript by using this (or
CoffeeScript's # shorthand)
As it turns out, CoffeeScript classes can be defined like:
class #Foo
which compiles to:
this.Foo = (function() {
function Foo() {}
return Foo;
})();
Assuming that foo.coffee is loaded before subfoo.coffee you can then do:
class #Subfoo extends Foo
Assuming, of course, that Subfoo needs be be assigned to the global scope. It's also worth mentioning that you'll need to expose your collections in a similar way. For example:
#Players = new Meteor.Collection 'players'
Also note that classes such as "Foo" are themselves a value, that you can assign to variables or put into a namespace yourself.
Using class #Foo is a great shortcut when you want to put that value directly into the global namespace.
But if you want to, you can also leave variables local and then add them to the global namespace yourself:
class Foo
...
Players = new Meteor.Collection 'players'
doThat = -> ...
_.extend(this, {Foo, Players, doThat})
Or if you'd prefer, you can have your "foo" module define just one global object foo that contains the exported values:
#foo = {Foo, Players, doThat}
Now modules that use the "foo" module can reference the values through the global variable foo:
class Subfoo extends foo.Foo
...
Or, if you'd rather be able to type just Foo even when exporting only foo, you can unwrap foo at the top:
{Foo, Players, doThat} = foo
class Subfoo extends Foo
...