-
Notifications
You must be signed in to change notification settings - Fork 4
CS2 Discussion: Question: New Feature Ideas #16
Comments
ExtensibilitySome combination of macros, compiler options and AST transformers would allow users to define their own dialects without having to create a fork. We have a community of language designers, and have let that become a force that fractures our work into tons of abandoned projects, each adding one or two features in isolation. Having a community of hardcore, programming language enthusiasts should be a positive thing. Using a parser generator will make our compiler more difficult to extend. A handwritten parser can be extended more easily. A dialect should be distributable as a compiler configuration, which would typically be a few KB of JSON. A lot of the new features people want are parsed identically to existing features. Prefix, infix and suffix operators are more or less generic. You just need to define the spelling, the precedence and what it compiles to. We can also generalise constructs like keyword-predicate-block, where again, you only need define a spelling for the keyword, the precedence of the expression as a whole, and what it compiles to. The predicate is a generic expression, so that will parse automatically, and the same is true for any block, so it's actually just a matter of defining what the header compiles to. We could probably just map new operators and other constructs to invocations, passing any operands, predicates and blocks along as lambdas, so the user only needs to provide a spelling, a precedence and a runtime function that can handle the call. I've explored actually implementing this, and it isn't that complicated. The only issue is wanting to change so many other things in the process, you end up with a totally new language :\ |
Pythonic ForI explained the logic behind this at some length in #11, but to summarise again here: If we allowed unpacking a sequence inside a for-loop when the names we're assigning are wrapped in brackets (
|
Named FunctionsThe grammars There is a proposal to add the same logic to ES6, so this feature may just magically start working in CoffeeScript soon! |
Object and Set ComprehensionsWe don't have these, and they're awesome. It's one of my favourite Python features that CoffeeScript lacks. |
Proper Equality OperatorAn operator that tests the equality of arrays, objects and sets, based on their contents, like every other scripting language has, so expressions like |
Falsey Empty CollectionsEmpty arrays, objects and sets should be falsey when used as a predicate. |
Operator OverloadingIt's a really useful feature, especially for library authors. |
Automatically Reference First ArgsThe first argument to any function should be assigned to a reserved name. Other dialects have used employees.forEach -> console.log it.name, "makes", it.salary I'd rather assign the arguments array to |
Pipe OperatorThere's a PR on the CS repo. |
Method OverloadingWould be great if method overloading was introduced, at least in the context of classes.
Just hypothetical code here - but in this scenario, by providing a username and password to one method, a Credentials object could be created before being passed of to another implementation of the method. The Credentials object could be used in multiple ways - converting itself to JSON, validating username or password strings on instantiation... There are better use case scenarios. In terms of code output, the above classes login method could end up looking something like below in ES 4/5 pseudo code:
There are better ways to do this, but there will be overhead from method lookup, but acceptable I think. In production code with a fixed strict spec, it would be possible to eliminate the redundancy of method overloading if the need wasn't presented, resulting in code with more direct access to functions without lookup required... No idea if this would work for standalone functions, but don't see why not... |
Enum
That would be somewhat similar to this in CS today:
Of course, hashes can be used instead
or
but in ES today, hashes involve implicit or explicit string comparisons.
and this is fine most of the time, but enums can also have methods, and are unique instance specific identifiers which cannot be reproduced anywhere else in code if not exposed. The example 'fun' implementation above would handle hashes or enums due to === checks used by switch statements, but enums would also allow for instanceof checks when appropriate, whereas string pollution (e.g. using strings instead of hashes) can scatter code in may cases. I haven't made a very strong case for enums here, but I do think they would be very useful in CS! |
AdoptsWe can't really do interfaces in ES/CS. but we can do mixins. How about this?
That defines an EventListener. Some classes may extend from it. Others may just need to borrow its abilities by using it as a mixin. If we have a class that extends from another, there is no other choice but to use a mixin to aggregate its behaviour additionally, but if we introduced an 'adopts' keyword instead of 'implements', we could mixin multiple classes as well
The reason I'm suggesting an alternative to the word 'implements' is that interfaces are generally type level commitments and their implementations rarely include executable code (though that depends on the language you're using, and there are shifts there too), but the emulation of interfaces is not really where CS is! An alternative would be to reimplement CS extends, such that the first class passed to extends must be a class, but subsequent additions will be regarded as mixins
The key problems are
From my POV, the word choices are 'implements', 'adopts', 'adapts' or 'uses', most likely favouring 'uses' as its declarative in relation to the implementing class, not its inheritance chain. Its not a straightforward thing to define, but I think if we put some thought into it, we could come up with a viable solution. Anyone else? |
Tuples
Just for example. Not proposing syntax. However, AFAIK, tuple like behaviour can be implemented in ES using Arrays and index transformations, so as a target language, ES can support them once CS transpiles.
I believe destructuring assignments can emulate, but I have to admit, I've failed to see how in exactly the same way? I'm sure tuple emulation must be possible in CS as is... would like some feedback on that! |
NaN is NaN
This way |
Generalized LoopsI talk about it in #11, but I would like to see generalized loops in CS that would handle Objects, Arrays, Strings and ES6 Iterables the exact same way. For backward compatibility, we could introduce a The syntax would look like: Examples: (console.log key, val) foreach key, val in {'foo': 'bar'}
(console.log idx, val) foreach idx, val in ['foo', 'bar']
(console.log idx, char) foreach idx, char in 'foobar'
(console.log count, item) foreach count, item in someIterrableObject |
Closing this thread as we have decided not to add any non-EcmaScript features. |
Though, I will point out that @carlsmith 's shalt project may be interesting to those wishing to experiment. |
... and should also mention that I am beginning work on a new language called LightScript, which may incorporate some of the suggestions on this thread (and from this repo generally). |
I know his is closed but I realized how awesome it would be to have a transpiler to other languages besides Javascript, including Java byte code and self contained binaries ;) But that's just me loving coffeescript so much. Give a man a hammer and everything starts looking like a nail... |
A few years ago I had to do some coding in PHP, and I stumbled across https://github.com/btwael/mammouth:
If only all languages had such things 😄 |
This issue was moved to jashkenas/coffeescript#4913 |
What new features would you love to see added to CoffeeScript?
Let's list them here, ideally one-idea-per-post, and then open new threads for features that garner interest.
The text was updated successfully, but these errors were encountered: