Code Monkey home page Code Monkey logo

Comments (7)

atsepkov avatar atsepkov commented on July 20, 2024

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.

valmynd avatar valmynd commented on July 20, 2024

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.

atsepkov avatar atsepkov commented on July 20, 2024

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.

valmynd avatar valmynd commented on July 20, 2024

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.

rjungbeck avatar rjungbeck commented on July 20, 2024

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.

 avatar commented on July 20, 2024

Supporting Python function definition is possible in javascript using the following trick: https://gist.github.com/amirouche/7515378

Good Luck.

from rapydscript.

atsepkov avatar atsepkov commented on July 20, 2024

Added @kwargs decorator and automating parsing of named arguments into a hash to handle this.

from rapydscript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.