Less verbose lambda syntax

About Monkey 2 Forums Monkey 2 Development Less verbose lambda syntax

Tagged: 

This topic contains 11 replies, has 4 voices, and was last updated by  Mark Sibly 2 years, 7 months ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #3663

    Samah
    Participant
    [/crayon]

    Thoughts, before I fork and pull request? 🙂

    #3665

    Mark Sibly
    Keymaster

    Probably not gonna accept it – I ‘m fine with current syntax and find ‘shortcut’ lambdas one of the hardest things to read in other languages.

    #3680

    Samah
    Participant

    Sorry, I’m not quite clear on your response. Was that “accept the pull request” or “accept the existing syntax”?
    One of the more powerful uses of lambdas/closures is to represent a very simple piece of logic (even one expression) as a first-class type. If the lambda in question is as simple as a comparison or equation, I think the condensed one-line syntax is quite acceptable.
    If the lambda has a lot of logic to it (event handlers, etc.) then it makes sense to spread it across as many lines as you need, as per the current syntax.

    #3683

    Mark Sibly
    Keymaster

    Sorry, that should have been ‘not’ – it was late.

    Maybe it’s just my inexperience with lambdas, but the short form looks like a minor ‘win’ at the cost of making the code look a lot more obsfucated. It’s just not very monkey-like IMO.

    Maybe it’ll grow on me (it has a tiny bit) but right now I just don’t see the need for it.

    #3687

    wiebow
    Participant

    TBH, I find this unreadable… If you want to return something, why not state it?

    #3688

    Samah
    Participant

    TBH, I find this unreadable… If you want to return something, why not state it?

    The idea is less about the implicit return statement and more about keeping a simple lambda on a single line.
    With a return statement and no inferred types it’s still quite legible. It just makes it much cleaner if you want to pass a single expression as a parameter to a function.

    [/crayon]

    Maybe it’ll grow on me (it has a tiny bit) but right now I just don’t see the need for it.

    If I add flatmap/filter/reduce to the Stack class and provide an example banana project, you’ll see the power of mini-closures and why it makes code much easier to understand. I’ve already added it, I’ll make a PR at some point.

    #3690

    Mark Sibly
    Keymaster

    Think I prefer ‘as opposed to’.

    The problem I have with the first one is it’s just a string of ‘stuff’. At least in c++ you get the enclosing ‘{‘ ‘}’ to delimit the lambda scope from the expression scope, even if it’s on the same line.

    #3691

    Samah
    Participant

    How hard would it be to tell the parser to make newlines optional for single statement lambdas, then? That should be a simple enough change, and if the developer prefers to split it across multiple lines, that’s up to them.

    #3721

    dmaz
    Participant

    At least in c++ you get the enclosing ‘{‘ ‘}’ to delimit the lambda scope from the expression scope

    isn’t that what Lambda() and End are for?

    #3725

    Samah
    Participant

    isn’t that what Lambda() and End are for?

    Pretty much, but the majority of the Monkey-X/MX2 community have an aversion to braces.

    #3752

    Samah
    Participant

    I’ll assume that’s a no, then.

    #3798

    Mark Sibly
    Keymaster

    Yes, that’s a no.

    I was initially planning on supporting ‘looser’ eol rules, but that wasn’t as easy as I thought it’d be.

    In the end, I sorted of just embraced the whole strict eol approach instead, thinking I might revisit it later. But now, I just don’t see a compelling reason to any more – I like it the way it is.

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.