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
Optional "new" to instantiate classes? #861
Comments
It would be possible to embed this behaviour in generated classes. The size of the generated code will be affected however, but it is something I do quite a lot come to think of it. I've also been seeing it more and more in node. Here's my way of handling
+1 |
Check out Coco, where you can define bound constructors for this particular effect. You can compare https://github.com/satyr/coco/blob/master/src/ast.co and https://github.com/jashkenas/coffee-script/blob/master/src/nodes.coffee to see how it enjoys |
It looks like since 0.9.5 classes are very similar to coco's. The new-free thingy would be a great addition, in my opinion. When I code js I always do this anyway. |
While |
Why "legacy"? Behaving differently for [[Call]] and [[Construct]] is perfectly reasonable. The culprit is that ES3/5 doesn't expose right APIs that handle this capability.
Newless constructors remove this overhead. Remember that current CoffeeScript classes do behave differently without |
"Legacy" in the sense of preexisting, or defined outside of Coffeescript. Poor wording on my part. To clarify my concern: in JS, calling a constructor without |
You can do Now that I think about it,
So either make it explicit or use a new language construct... or leave it as is and let devs sort it out. |
We don't write
Concerning about changing users' habitude is pointless for Coffee, which has done plenty of harm in that regard already (e.g. |
Sure, but this isn't about consistency between CS and JS. It's about consistency within CS itself. This proposal entails taking a piece of syntax that already causes confusion -- |
CS can never get rid of this responsility as long as it compiles into JS. Making |
I think that sethaurus' arguments here are really sound. I agree entirely. |
Given the pull request that would invalidate
That being said, i don't like saying "it's impossible" without providing alternative solution(s). JS-style classes can still be used for this kind of things. Stealing @davidchambers' example, with JS-style classes: Point = (x, y) ->
return new Point x, y unless this instanceof Point
@x = x
@y = y
# Let's add a method to make it more interesting.
Point::add = (point) ->
Point @x + point.x, @y + point.y
p = (Point 4, 5).add Point 6, 7 Or, if you really abhor JS-style classes and really really want to have # Auxiliary function to make classes that can be instantiated without `new`.
newLess = (ctor) ->
F = (args...) ->
return new F args... unless @ instanceof F
super
F extends ctor
F
Point = newLess class
constructor: (@x, @y) ->
add: (point) ->
Point @x + point.x, @y + point.y
p = (Point 4, 5).add Point 6, 7 Yes, quite a bit of magic. But, on the other hand, the |
class WithBoundConstructor
constructor: =>
alert @ instanceof constructor
WithBoundConstructor() # alerts true turned down several times :p. |
Is this a good idea? Currently if you instantiate a class without "new" it just returns undefined.
Or should this be left for the developer, since now we have executable class bodies?
The text was updated successfully, but these errors were encountered: