Comments (13)
I think forbidding access to the function variable is probably correct. It just sort of happens to work by chance of implementation currently.
I think in cases where you need to reference the parameter using an explicit arrow function is the simplest and most direct way to do it.
from civet.
Sounds good. I think we can turn $
into a Ref to achieve this?
from civet.
@edemaine Yes, that should work.
from civet.
I didnt realize something like &+&
doesn't work - that is counterintuitive.
For multiarg probably the closest precedent in mainstream langs is swift - which allows $n
eg. numbers.reduce(0) { $0 + $1 }
which is nice but I too think its not that much better than just having an inline lambda with named args.
from civet.
Huh, I was wondering about wrapping &
expressions (or some other notation) in braces to indicate where the function wrapper goes (especially for placeholders). But yeah, not much more concise than an arrow function. I guess the nice thing is not having to spend time naming the arguments and not having to repeat them more than once... So I think it still might be interesting to think about.
from civet.
I was wondering about wrapping & expressions (or some other notation) in braces to indicate where the function wrapper goes (especially for placeholders)
Yeah I think most of the ambiguity here is around identifying where the function boundary goes. I propose simply mandating that the function is explicit (similar to my other suggestion in the do discussion)
ie. [1, 2, 3].map(new User id: &)
-> [1, 2, 3].map(-> new User id: &)
If this seems too big of a change, then can we separately adopt it
as kotlin supports it ? If a function doesn't have any explicit arguments, it gets an implicit argument called it
which is the solo argument.
It is also convenient for nested lambdas, because it
always comes from the nearest parent function without an explicit argument:
[1, 2, 3].map(-> foo(name -> `name${it}`))
Transpiles to:
[1,2,3].map(function(it) {
return foo(function(name) {
return `name${it}`
})
})
from civet.
The $
ref part of this was fixed in 1d9e4d0
from civet.
It might not be too hard to get &
as a topic reference in places where it is unambiguous. It may require some clever lookaheads in some other situations.
from civet.
Just had a thought that maybe we could have a syntax like &.
but which creates a new this
scope so that @
is the topic reference.
@.something // if we want to force accessors into @something only
---
function(){ return this.something }.call
We could maybe use a different symbol as well.
from civet.
Did a quick search of the CoffeeScript codebase and @.
appears zero times so it's a good candidate. We could disable it in coffeeCompat
as well.
from civet.
items.map @\ `${@name}: ${@price}`
animals.forEach @\ console.log 'test' if @name == 'Fluffy'
Seems ok, but still might need to refine the symbol.
from civet.
Neat idea! The two issues I see are that it shadows this
in the surrounding context (might be annoying in some cases), and that @
already means something when followed by a [
, (
, or binary op (and .
too), so the notation feels limited.
I wonder whether something like { @.foo }
could be shorthand for function() { return this.foo }.call
? This has the advantage that you can actually specify the block boundary, e.g. { 'hello' + @ }
works. The only conflict I know is object literal syntax like { @foo }
but I believe that doesn't work with a dot. Oh, no, it does, with the new object literal shorthand. Also { @(5).x.y }
is an object literal now I think.
from civet.
This is resolved by (now implemented) #1070: multiple &
s now refer to the same argument.
from civet.
Related Issues (20)
- Ampersand bugs HOT 1
- `.#x` shorthand for private properties HOT 1
- Missing parens in `.-1` access in certain contexts
- Attempted pattern matching ternary HOT 1
- Finishing civetconfig
- `get #x` requires newline, not semicolon HOT 1
- coffeeDo bug
- Use `{}` instead of `;` for empty block
- `for`/`else` HOT 2
- Generator operators
- Cannot shadow operator with argument HOT 1
- Sourcemaps are off when used with @propName HOT 1
- Add source location to error nodes
- `-o dist/.js` errors if the `dist` folder doesn't already exist HOT 2
- `-version` with only one hyphen throws a ReferenceError HOT 1
- "Invalid command-line argument" error includes stack dump
- Splice assignment with loop
- Empty block following pattern in switch expression generates invalid JS
- hoistDec bug
- Pipeline placeholder ^^ or %% or `|x>` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from civet.