Skip to content

blockexpr discussion #2

@JanSharp

Description

@JanSharp

I am planning on introducing a new expression which is basically just a statement list. You can think of it like

local foo = (function()
  print("hello world!")
  return 1
end)()
print(foo)

except that the syntax would be

local foo = do
  print("hello world!")
  return 1
end
print(foo)

Except that the return in this case returns from the entire function, not from the blockexpr.
(Refer to this issue for discussion about which keyword to use)

Honestly the fact that this needs a new keyword is a gigantic downside, but if we can find a decent keyword for it I consider it worth it.

Since we are also still within the same function that the expression is in, keywords like break and goto can actually link to loops and labels outside of the expression. I thought about it and I don't see any issues with this.

The inspiration for this expression is that it is just a simpler form for the imo disgusting syntax for immediately invoked function expressions which is (function() return expr end)()... or worse (() => expr)().
We already use the former, with lambda expressions we will do the latter, and ultimately I believe it is worth improving this syntax.

Additionally, lambda expressions can be simplified thanks to this blockexpr by simply always expecting an expression. In fact, lambda expressions would just end up being syntatic sugar for function() return <expr> end. Think about lamba expressions like

  • (foo) => print(foo)
  • () => do print("hi") return 1 end

It just works.

Lastly note that this will always - somehow - be an opt in language feature due to the introduction of a new keyword which can potentially break Lua code.

That all said, I'm asking for feedback on this, if it is feature creep or not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions