-
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
Splitting CS into Language plus Standard Library #2053
Comments
A small note. The "loop" construct already exists; it's just kind of buried in the docs. See #2039 (comment) for suggestions on improving the docs. |
@alexkg This is an interesting proposal, but I think there are at least two show stoppers. First, I think many folks like the current "class" syntax, as it allows you to express object factories with a syntax that is very nice and familiar to Ruby/Python programmers. Having said that, and despite having a lot of experience in Python/Ruby myself, I tend to avoid "class" in CoffeeScript, as I prefer the function closure style where a function returns a new object with functions that act on closed variables. So, I am certainly intrigued by your proposal, just doubtful it will fly with others. I'm not sure how integral this feature is to your proposal, but I really don't want objects to be wrapped:
I'm -1 on the proposal as it stands, but I hope it leads to some thoughtful discussion. I think I understand the gist of what you're after. Basically, you're saying that a slightly more lean syntax that's focused on objects and functions can ultimately be more flexible/extendible in terms of creating objects, without prejudicing you toward a standard notion of "class" as a way to construct the objects, correct? Or is it something else you're after? |
Hi @showell thanks for your comments. Your understanding is correct and you're right that it won't suit everyone. Je suis un minimaliste, I suppose. I guess the question is how much the core language be improved (in terms of operators, syntax, etc) to build these kinds of syntactic abstractions in a DSL kind of way. The object to new function translation is a bit more of a tangent, just something I have been thinking about for nested objects:
And as above, it might help clean up a library based class syntax. BTW I would actually like to get rid of |
Nope. It is impossible to do classes right as a library, and we've seen this countless times in the JavaScript world. That is why this is a language feature, and I believe it should continue to be. We've also written tons of code on CoffeeScript classes by this point, so simply removing them and replacing with a half-baked library is not a desirable option on all fronts. |
I guess the question can be rephrased as: what's missing from JS that makes writing classes "impossible"? Why not focus on these issues, making CS more versatile at a base level rather than providing higher level syntactic abstractions? I think some of the things like monocle mustache and |
I hate closing such a thoroughly written issue, but with CS2 now outputting ES |
TL;DR
Arguing for the removal of
class
,extends
, and (preemptively)extended
, to be replaced by a standard library, possibly supplemented with other syntactic constructions:Introduction
This is a kind of "philosophical issue" concerning the direction of CS.
It mainly concerns constructs in CS like
loop
,class extends
, and if it gets included, theextended
hook.My main problem with the
extended
hook is that it introduces higher-level machinery into the language that should be provided by the program itself. In doing so, CS becomes separate to JS – in addition syntax translation, the language has baked in assumptions about its environment. This "proposal" is about enriching the language at the base level to make it easier to handle common patterns likeclass extends
andextended
outside the language.Background
Classes in CS implement a very constrained idea of inheritance. As a purely syntactic construction, trying to implement mix-ins or other extensions gets clunky:
The proposed
extended
hook goes some way towards solving this problem...However the
extended
hook seems like it's compensating for the deficiencies ofclass extends
, of having what should be a library construct baked directly into the language. Since we can't build onextends
as a function, we have to extend the language itself to get what we want.Instead of elaborating some kind of class mechanism in CS, I think the emphasis should be on the lower level elements that can support this kind of behaviour in a library.
What does the current system provide?
Named functions:
Class extension:
Clean definition syntax:
Executable class bodies:
And most importantly, super:
How can this be implemented as a library?
Named functions:
Reintroducing named functions is possible, but controversial, but essentially non-essential.
Class extension:
The
extends
functionality is already external to the language.Clean definition syntax:
The current syntax is certainly neat, but nothing that couldn't be arrived at in another way:
Executable class bodies:
As extends can be written as a function, we can just use a function for our definition:
Using objects as definitions:
Using object literals instead of objects can make things a bit cleaner:
It just means out prototype variables have to be last.
The super reference:
This is the most difficult part, since it's hard to figure out if a given function is class-bound, in which case we want
this.__super__
, or if it's prototype bound, in which case we wantthis.constructor.__super__
.Short of specifying exactly which one, we could just use the
@::
syntax above to distinguish prototype and class functions.What features could be added to support class-extends?
Executable object literals
If object literals themselves were translated from:
To:
Then we'd have a bit more flexibility in how we define our class:
(Here,
extend
executes the definition on the prototype.)Or even:
This also presumes the existence of a
@@
operator meaningthis.constructor
.Mustache
If the chaining vs cascading syntax gets nailed, we wouldn't even need abstraction:
Would compile to:
Loop
If there's an argument for extracting language features into functions, I think
loop
definitely qualifies:Gets translated to:
It's another keyword for what is a dubious control structure in the first place...
It can be extracted into a function quite easily:
Or for the asynchronous among you:
Which really underlines the argument of having these kinds of things as library functions rather than rigid language-bound constructs.
The text was updated successfully, but these errors were encountered: