Project

General

Profile

Feature #16898

Modify the syntax of -> lambda expressions in ruby3

Added by jackmaple (maple jack) 7 months ago. Updated 7 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:98406]

Description

Hello ruby team.
Can we modify the syntax of lambda expressions in ruby3?
The current definition of lambda expression was added in ruby1.9, but this syntax looks too tedious.

lambda {|x| x + 1}   #This looks elegant.
#but...In lambda expression body, when there is only one line of statement
-> x {x + 1}    #Can the braces be removed?
-> x do x + 1 end  #As above, can the do ... end block be removed?

So are we going to abandon some historical issues? To modify -> lambda syntax?
It looks like this:

x -> x + 1
(x,y) -> x + y
x -> y -> x + y
x -> {
   x + 1
   x + 2
}

I think do..end or braces are only needed when there are multiple lines of code,lambda expression if there is only one statement should not need parentheses or do ... end block.
Thanks.

#1

Updated by jackmaple (maple jack) 7 months ago

  • Description updated (diff)
#2

Updated by jackmaple (maple jack) 7 months ago

  • Description updated (diff)

Updated by matz (Yukihiro Matsumoto) 7 months ago

"Ugliness is in the eye of the beholder". Could you explain why the current syntax is ugly?

In any case, if we change the syntax, all Ruby programs that use the current ugly lambda syntax no longer work. How do you think? Is it worth the pain of the whole Ruby community?

Matz.

Updated by shan (Shannon Skipper) 7 months ago

To me, this

-> lightness { lightness }

Is prettier than

lambda { |lightness| lightness  }

¯\(ツ)

I like stabby lambdas, but it doesn't seem worth the pain of the massive code breakage even if we all agreed they're ugly.

Updated by 0x81000000 (/ /) 7 months ago

matz (Yukihiro Matsumoto) wrote in #note-3:

"Ugliness is in the eye of the beholder".

-> (;x) { x = 1 } # correct
->  ;x  { x = 1 } # syntax error

Is the second uglier?

Updated by jackmaple (maple jack) 7 months ago

matz (Yukihiro Matsumoto) wrote in #note-3:

"Ugliness is in the eye of the beholder". Could you explain why the current syntax is ugly?

In any case, if we change the syntax, all Ruby programs that use the current ugly lambda syntax no longer work. How do you think? Is it worth the pain of the whole Ruby community?

Matz.

Hello, we currently have lambda syntax, but at present-> seems to be just a simplified alternative,-> The braces behind cannot be omitted when there is only one line of statement, maybe we can add a new lambda syntax, thank you

#1 -> x {x}
#2 x -> x

I think the second way of writing can omit curly braces in only one expression.Of course this is just my suggestion,I respect your decision.thank you very much.

Updated by shyouhei (Shyouhei Urabe) 7 months ago

Not going to join the ugliness debate but I doubt if the proposed new syntax technically possible. It would be much convincing if you provide a working pull request.

#8

Updated by jackmaple (maple jack) 7 months ago

  • Description updated (diff)
#9

Updated by jackmaple (maple jack) 7 months ago

  • Description updated (diff)

Updated by jackmaple (maple jack) 7 months ago

Maybe my expression has some problems, sorry, it has been modified, in fact, what I want to say is that the existing lambda expression looks like the arrow symbol is not very intuitive.
If we can simplify the grammar, in some cases it would be better to remove the braces?

-> x x   # error
-> x {x} # correct

So it would be better if we could provide a grammar with braces removed?In order to maintain compatibility, it is not necessary to modify the existing grammar.
For example, add a new lambda operator "==>"?

-> x {x}
lambda {|x| x}
x ==> x     # suppose

What do you think?
thanks.

Updated by matz (Yukihiro Matsumoto) 7 months ago

I get your point. But to my eyes, ==> is even uglier. It's easily confused with other operators which do not correspond with lambdas but association operators (hash rockets) or comparison operators.
I am not yet convinced having a new lambda operator without braces around the body is that important, despite the possibility of the above confusion.

Matz.

Also available in: Atom PDF