question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question about feasibility: option to use JS function syntax for nested functions, especially inside dicts/object-literals

See original GitHub issue

I would like to be able to ~mix function syntaxes a bit~ try allowing JavaScript function syntax for [nested] anonymous functions. Has that been explored before? Any warnings before I explore it?


Context:

In the RapydScript code in the README, I find the nested/inlined anonymous functions difficult to read. I understand the reasoning, and I “get” the ; workaround/idiom. I would just be very curious how RapydScript looks/feels if it allows for optional curly brackets on anonymous functions. If needed, maybe those could be with function as the keyword instead of def? That would feel like a good compromise to me.

(Alternatively, the braces version of function syntax could be arrow-function style like (foo) => { return ... } … which might be good for certain libraries’ APIs.)


I can take a stab at prototyping this, to see what we think. I just figured I would ask about it before I start throwing code around 😸

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
valq7711commented, Jan 7, 2022

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?

1reaction
atsepkovcommented, Jan 6, 2022

Sorry, there should be a comma between the 2 arguments I missed:

takesTwoCallbacks(def(): return 2 + 2;, def(): return 3 ^ 2;)

On Wed, Jan 5, 2022 at 11:02 PM Alexander Tsepkov @.***> wrote:

I tend to err on the side of minimalism in language design, and not a fan of multiple syntaxes that do the same thing.

What would be the use case that can’t be accomplished with the current syntax?

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:

takesTwoCallbacks(def(): return 2 + 2; def(): return 3 ^ 2;)

On Wed, Jan 5, 2022 at 10:32 PM Michael Floering @.***> wrote:

I would like to be able to mix function syntaxes a bit, at least, using braces on inline/nested functions. Has that been explored before? Any warnings before I explore it?

Context:

In the RapydScript code in the README, I find the nested/inlined anonymous functions difficult to read. I understand the reasoning, and I “get” the ; workaround/idiom. I would just be very curious how RapydScript looks/feels if it allows for optional curly brackets on anonymous functions. If needed, maybe those could be with function as the keyword instead of def? That would feel like a good compromise to me.

(Alternatively, the braces version of function syntax could be arrow-function style like (foo) => { return … } … which might be good for certain libraries’ APIs.)

I can take a stab at prototyping this, to see what we think. I just figured I would ask about it before I start throwing code around 😸

— Reply to this email directly, view it on GitHub https://github.com/atsepkov/RapydScript/issues/247, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATOR3PCSYMSGD5O5HWNZQ3UUUEOXANCNFSM5LLMUYJQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript Nested functions - GeeksforGeeks
Here the task is to create nested functions, JavaScript support nested functions. In the examples given below the output returning is a ...
Read more >
JavaScript Nested function - Stack Overflow
Creating a function within another function changes the scope of the function in the same way it would change the scope of a...
Read more >
4 Examples of Javascript Nested Functions - eduCBA
Guide to Javascript Nested Functions. Here we discuss How do Nested functions work in JavaScript and Examples with codes & outputs.
Read more >
Variable scope, closure - The Modern JavaScript Tutorial
Nested functions. A function is called “nested” when it is created inside another function. It is easily possible to do this with JavaScript....
Read more >
How to use Nested Functions in JavaScript, Online Course
Access the entire JavaScript online course at Zenva Academy ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found