Comments (7)
from rapydscript.
For object-literal's functions defined using indentation comma is optional, so you can:
obj = {
foo: 1,
bar: 2,
one: def():
return 1 # semicolon and comma are not needed
two: def():
return 2
three: 3,
four: 4
}
or even:
params = {
width: 50,
height: 30,
def onclick(self, event): # note that `self` is required as first arg
alert("you clicked me")
def onmouseover(self, event):
$(self).css('background', 'red')
def onmouseout(self, event):
# reset the background
$(self).css('background', '')
}
if you still need extra begin/end tokens you can always use parentheses:
params = {
width: 50,
height: 30,
onclick: (def(event):
alert("you clicked me")
),
onmouseover: (def(event):
$(this).css('background', 'red')
),
onmouseout: (def(event):
# reset the background
$(this).css('background', '')
)
}
factorial = (def(n):
if n == 0:
return 1
return n * factorial(n-1)
)
...to have clear open/close, whereas
;
-or-newline can't provide that as well in a nested context.
I totally agree about ;
, but what's the problem with newline+de-indentation? This is almost the same as in the class definition, no?
from rapydscript.
from rapydscript.
Yes indeed
There is already a way to inline an anonymous function with more logic
after it by using a semicolon, if that's what you're looking for
Right! The RapydScript README informed me of that functionality. It makes enough sense to me. I just believe this part of the syntax will be a turn-off for some people — and on a team of multiple programmers, it could be a deal-breaker for at least one of them. Does that make sense?
So I'm not making any critique of the existing functionality, rather I'm trying to say, there might be another syntax worth considering [perhaps in some contexts or when a compiler flag enables the option].
I'll use an example!
Here's one example from the README:
params = {
width: 50,
height: 30,
onclick: def(event):
alert("you clicked me"),
onmouseover: def(event):
$(this).css('background', 'red')
,
onmouseout: def(event):
# reset the background
$(this).css('background', '')
}
I would really like the option (even if it's a compiler flag, rather than a contextually-allowed/idiomatic thing) ... to use a syntax like this:
params = {
width: 50,
height: 30,
onclick: function (event) {
alert("you clicked me")
},
onmouseover: function (event) {
$(this).css('background', 'red')
},
onmouseout: function (event) {
# reset the background
$(this).css('background', '')
},
}
params = {
width: 50,
height: 30,
onclick: function (event) {
alert("you clicked me")
},
onmouseover: function (event) {
$(this).css('background', 'red')
},
onmouseout: function (event) {
# reset the background
$(this).css('background', '')
},
}
Comparing the semicolon example — the README shows one like this:
hash = {
'foo': def():
print('foo');,
'bar': def():
print('bar')
}
// variation on that - nested
def example():
obj = {
'foo': def():
print('foo');,
'bar': def():
print('bar')
}
return obj
well, compare with:
obj = {
'foo': function () {
print('foo')
},
'bar': function () {
print('bar')
},
}
def example():
obj = {
'foo': function () {
print('foo')
},
'bar': function () {
print('bar')
}
}
return obj
Do you kinda see what I mean?
Even though it is a compromise of one principle, it's a trade-off ... A trade-off could be worth it, if the resulting RapydScript developer experience is a bit more Pythonic (arguably) thanks to less ambiguity/ less cognitive overhead. (I think it takes less cognitive overhead/ it's less ambiguous... to have clear open/close, whereas ;
-or-newline can't provide that as well in a nested context.
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
factorial = def(n):
if n == 0:
return 1
return n * factorial(n-1)
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
factorial = function (n) {
if n == 0:
return 1
return n * factorial(n-1)
}
I'm also partial to this "unmodified" option, I guess... rather than devising a middle-ground syntax (in this case), I'm essentially wanting to be able to use the Python function syntax at the top level, with the option to use function () {}
style [at least in some contexts].
Humbly submitted 😄 thank you for your amazing project!
from rapydscript.
@valq7711 This is great, it resolves the issue for me. I'm going to respond in two comments.
I totally agree about
;
, but what's the problem with newline+de-indentation? This is almost the same as in the class definition, no?
I see what you mean in comparing it to a class definition, but IMHO it's different because a class definition does not occur inside curly braces / dict / object-literals.
I think the other context is Python's syntactic sugar for tuples, and the notorious gotcha associated with that (i.e. that return 1,
means return (1,)
). I know this isn't news! RapydScript is aware of and this, and it informed RapydScript's design decisions about these parts of the syntax.
As a side-effect of watching out for return 1,
etc in Python, I think I've been trained to see a "stray" or "inconsistent" ,
as a bug. So, it takes cognitive overhead to "turn that off" — seeing that or something like it — when reading RapydScript code.
from rapydscript.
if you still need extra begin/end tokens you can always use parentheses:
params = { width: 50, height: 30, onclick: (def(event): alert("you clicked me") ), onmouseover: (def(event): $(this).css('background', 'red') ), onmouseout: (def(event): # reset the background $(this).css('background', '') ) } factorial = (def(n): if n == 0: return 1 return n * factorial(n-1) )
This is a great compromise, thanks for showing me this alternative. I didn't realize (def(): ...)
would be valid syntax!
from rapydscript.
Thank you for the help @atsepkov and @valq7711. Really appreciate it!
Maybe the README could be updated to include an example of the parens syntax. (edit: I've just opened a quick draft PR)
from rapydscript.
Related Issues (20)
- append method does not compile to js push method HOT 1
- VSCode extension for syntax highlighting HOT 4
- cannot compare string HOT 3
- Typescript has problem compile RS --es6 output at class attributes HOT 2
- Why not support in-browser compiler? HOT 2
- enhancement: check for already reserved words like: enum HOT 18
- Cannot resolve python imports HOT 8
- Is that possible to parse python code to js AST? HOT 1
- fstring support. HOT 1
- How to keep all comments HOT 8
- Can't find python module that is installed and available? HOT 1
- Cannot catch ImportError HOT 2
- Cannot import from custom module in different directory HOT 5
- How to use RapydScript with JavaScript MutationObserver HOT 1
- How to run code snippets from tutorials on old Android? HOT 1
- Computed and negative slices
- parenthesized syntax HOT 2
- TypeError: undefined is not a function HOT 1
- Compiler issues HOT 1
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 rapydscript.