-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
super should be accessible within sub-functions of class methods #1606
Comments
... but what function do you expect it to call? It is an anonymous function after all. |
The same one as if I didn't have the setTimeout function wrapping it. I edited the example and put in a class it extends (whoops). So in this example |
Can you give a suggested compilation for the sample code in your original post? |
Sure... Starting to realize it might be a little more complicated than I initially thought because you'll need access to
and here is one using a bound function using only an
I think this would be useful for a few cases, so let me know if you think it is worth the cost of those two extra variables or if you have a better way of doing this. |
It feels like - especially in a case where the fat arrow is used - super should be available to call the method. I would give a vote to this. |
This is the same way They're doing this: module ActiveRecord
module Callbacks
# ...
def create
run_callbacks(:create) { super }
end
end
end Also check out Yehuda Katz post on The current workaround for using class Parent
method: ->
console.log arguments
console.log "parent method called"
class Child extends Parent
super: -> @constructor.__super__
method: ->
console.log "child method called"
args = arguments
self = @
@another -> self.super().method(args...)
another: (callback) ->
console.log "another child method called"
callback()
object = new Child
object.method("Hello World") That's pretty much what Katz is saying you'd have to do if Ruby didn't support calling At first glance it seems like calling +1 for this feature! |
+1 on this, especially when using => |
+1, but I'd like super to be lexically scoped and independent of ->/=>. |
got something sorta working, methinks. |
On second thought, I think it would be better to change This is more Pythonic, where you have to declare which method on super you are calling, e.g. class Foo extends Bar
constructor: ->
super.constructor()
method: ->
super.method() But it's more aligned with the nature of Javascript. Consider that decorated functions cannot know its own name at runtime: class Foo extends Bar
method: decorator ->
super # the compiler could figure out that `method` is being called
# but is it really worth it?
super.method() # simple to implement, easy & consistent. |
I'm going to change |
+1 on case with fat arrow |
another +1 on case with fat arrow |
+1 just ran into this today. Not being able to use super requires falling back to standard ugly javascript prototype calls whenever you need to wrap something in an asynchronous call. |
This is a great idea. But I'm a bit confused -- why should http://coffeescript.org/#try:class%20Foo%20extends%20Bar%0A%20%20baz%3A%20-%3E%0A%20%20%20%20super So shouldn't |
I think it should work in both cases. Just feels especially relevant with fat arrows. |
This appears to be closer to correct in 0.3.3 as released today. Thanks! At least it doesn't throw an error now... The example above compiles into this: Test = (function(_super) {
__extends(Test, _super);
function Test() {
return Test.__super__.constructor.apply(this, arguments);
}
Test.prototype.method = function() {
return setTimeout(function() {
return Test.__super__.method.apply(this, arguments);
}, 1000);
};
return Test;
})(Foo); Which is correct, except the There is also a bug if the function is assigned to a variable where the wrong class Test extends Foo
method: ->
foo = ->
super
foo() compiles into: Test = (function(_super) {
__extends(Test, _super);
function Test() {
return Test.__super__.constructor.apply(this, arguments);
}
Test.prototype.method = function() {
var foo;
foo = function() {
return foo.__super__.constructor.apply(this, arguments);
};
return foo();
};
return Test;
})(Foo); which uses Again, I can file that one separately if you like. |
Great thoroughness, @devongovett! |
+1 This bit me today, too. In particular, it'll bite you if you try to store a reference to a function that invokes class Foo extends Something
bar: ->
func baz: => super Which generates: Foo = (function(_super) {
__extends(Foo, _super);
function Foo() {
return Foo.__super__.constructor.apply(this, arguments);
}
Foo.prototype.bar = function() {
var _this = this;
return f({
baz: function() {
return baz.__super__.constructor.apply(_this, arguments);
}
});
};
return Foo;
})(Something); Clearly, there is no value As a work-around, I found that you can get an anonymous function that invokes the current method's class Foo extends Something
bar: ->
args = arguments
callback = do (args...) =>
=> super(args...) |
Yes, calling super from sub-functions would be definitely great! But (largely) based on @pvande 's tip, I now use a one-liner : class Foo extends Something
bar: ->
call_super = do (args=arguments) => => super(args...)
baz = ->
call_super()
baz() Strange the first time, but it's not so bad and does trick. |
If you use the Cardamom clazz system, it gives you https://github.com/jaekwon/Cardamom/blob/master/src/clazz.coffee Foo = clazz 'Foo', Bar, (_super) ->
bar: ->
baz = =>
@super.method() You do need to spell out the method though. |
+1 for
|
+1 I completely agree on |
+1 ... hmm... no progress or comments on this for a while. Is there a hitch? |
+1 Would be good |
+1 Having to resort to |
@Blasz couldn't you have resorted to a |
@lydell I was referring to |
Hm, but |
This works in CS2: class Test extends Foo
method: ->
setTimeout ->
super()
# …
, 1000 |
Right now the following CoffeeScript generates the error "cannot call super on an anonymous function":
This makes sense for anonymous functions outside the scope of a class, but for those within class methods, like the one above, I think super should still be accessible. How difficult would this be to implement?
The text was updated successfully, but these errors were encountered: