Comments (7)
These are two separate features you're talking about here. The default values for functions are indeed implemented in RapydScript, and that is the feature you're showing in your first example. The second feature, keyword arguments, is not there. It's not broken, it's simply not implemented, perhaps RapydScript should throw an error here instead.
The reason the second feature isn't implemented is because the problem is not as simple as it seems, due to the nature of duck-typing. Your solution, for example, introduces multiple special cases and in my opinion is worse than the problem it solves.
Let's me modify your example to make these special cases more apparent:
def Vehicle(doors=2, wheels=4, windows=4):
console.log(doors, wheels, windows)
localScope = def():
Vehicle = def(make, model):
return "make is " + make + " and model is " + model
car = Vehicle(windows=6)
What should that compile to? The function that seems most logical to reference here is local, yet the argument signature doesn't match. Here is another even more common case:
def Vehicle(doors=2, wheels=4, windows=4):
console.log(doors, wheels, windows)
def Horse(windows=0):
console.log("Neigh!")
if somevar:
transport = Vehicle
else:
transport = Horse
transport(windows=6)
How is the compiler supposed to handle this?
from rapydscript.
About your first example:
What should that compile to? The function that seems most logical to reference here is local,
yet the argument signature doesn't match.
If the argument signature doesn't match, imho it should be looked if the called function contains something like *_kwargs in the original signature (which would be another another thing that would be very nice if RapydScript would support that (calling functions with signatures containing *_kwargs could be translated as if a dict-like parameter would've been passed containing the arguments that are not otherwise part of the function signature)). Otherwise, it shall throw an Exception / Compiler Error.
About your second example: I agree, it would be cumbersome to support this behaviour - maybe it would be appropriate to not allow named arguments for scenarios in which the function signature is not obvious.
I want to emphasize how great it would be if RapydScript would support named arguments, for me not having that feature was the biggest annoyance when writing JavaScript. The way this works in Python not only improves readability, but also is very convenient as one doesn't have to remember the order of the parameters every time when writing a function call.
from rapydscript.
You could simulate this feature via JavaScript's object literals:
def fun(kwargs):
default = {
foo: 1,
bar: "baz"
}
for arg in kwargs.keys():
if not kwargs[arg]:
kwargs[arg] = default[arg]
...
fun({foo: 5})
While it might be nice to have RapydScript compile into that automatically, I'm not sure I want to do that yet. It would make function declarations harder to analyze in source code (keep in mind that generated source code would not look as clean as the above code, I can't use keys
since stdlib could be omitted), and would introduce slight overhead to every function call that JavaScript doesn't experience. Perhaps a separate compile flag would be suitable for this, like with namespace imports.
from rapydscript.
Yeah, such a compile flag would be nice.
But because of the performance concern: What I had in mind was that RapydScript could manage a list of known function signatures and if the signature is known, the function calls get adjusted to the order in which they are expected -> Vehicle(windows=6) becomes Vehicle(null, null, 6), thus still compact and readable code and no (runtime) performance penalty.
Partly to provide such function signatures (and also to be able to share more code between JS and Python, of cause), I ported some of the JavaScript Standard Libraries to python:
https://github.com/valmynd/jslib
https://github.com/valmynd/jslib/blob/master/jslib.py
Maybe this could be helpful?
from rapydscript.
Why don't you take the presence of keywords arguments in function/method definitions and calls as trigger to use object literals. This would spare the function table and the global flag. If not, I would suggest to use a decorator (but this means that you need to know the function definition when generating calls for it)
from rapydscript.
Supporting Python function definition is possible in javascript using the following trick: https://gist.github.com/amirouche/7515378
Good Luck.
from rapydscript.
Added @kwargs decorator and automating parsing of named arguments into a hash to handle this.
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
- Question about feasibility: option to use JS function syntax for nested functions, especially inside dicts/object-literals HOT 7
- 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.