Feature #8361

Alternative syntax for block parameter

Added by Alexey Muranov 12 months ago. Updated 11 months ago.

[ruby-core:54749]
Status:Closed
Priority:Normal
Assignee:-
Category:syntax
Target version:-

Description

=begin
I propose an alternative syntax for block parameters:

p = lambda [x] {
# do whatever you like with x here, but be sure to return the result
}

instead of

p = lambda { |x|
# ...
}

This would be consistent with the syntax of procedure call: (({p[a]})).

Also, compare:

a_bunch.each [element] do
# ...
end

with:

a_bunch.each do |element|
# ...
end

=end

History

#1 Updated by Nobuyoshi Nakada 12 months ago

If it were introduced, how could you pass an array with a block?

#2 Updated by Nobuyoshi Nakada 12 months ago

  • Category set to syntax
  • Status changed from Open to Feedback

#3 Updated by Alexey Muranov 12 months ago

=begin
You are right, there is a problem. It is similar to the problem with hashes:

def f(h); end
f {1=>2}
# => SyntaxError: unexpected =>, expecting '}'

It can be worked around in the same way:

foo([1, 2, 3]) [x] {x*x}

But there will be incompatibilities.
I will think more about it.

P.S. The problem appears only if the array is the first argument, otherwise it would be clear because of the absence of comma.
=end

#4 Updated by Alexey Muranov 12 months ago

=begin
From the top of my mind, i can propose only this:

p = lambda [x]->{
# ...
}

[2, 3, 4].map [x]->{x*x}.reduce [0, 1] [m, o]->{m << o}

(i didn't like the new lambda syntax anyway:) ).

Another possibility:

p = lambda [x]:{
# ...
}

collection.each [element] : do
# ...
end

[2, 3, 4].map [x]:{x*x}.reduce([0, 1]) [m, o]:{m << o}

(This might conflict with the ternary expression.)

The current syntax for comparison:

[2, 3, 4].map {|x| x*x}.reduce([0, 1]) {|m, o| m << o}
=end

#5 Updated by Nobuyoshi Nakada 12 months ago

=begin
It's unacceptable to distinguish two semantics of "(({[ ]}))" by a token following ((after)) it.
=end

#6 Updated by Yukihiro Matsumoto 12 months ago

I don't think the syntax cannot be defined without conflict.
Prove me I am wrong.

Matz.

#7 Updated by Alexey Muranov 12 months ago

=begin
I agree, it is getting complicated, maybe it was a bad idea. It may be closed if you do not think it is worth consideration.

The best i could think of was to allow to use some symbol, the colon for example, to separate explicitly the block from the list of argument:

p = lambda : [x] {
# ...
}

collection.each : [element] do
# ...
end

But it is getting complicated and not very readable.
=end

#8 Updated by Nobuyoshi Nakada 12 months ago

It conflicts with ternary expressions, and symbol literals.

You should abandon use of colon and brackets.

#9 Updated by Alexey Muranov 12 months ago

=begin
Nobu: i do not think the conflict with ternary expression is the worst part: i think that if in ambiguous cases the colon was interpreted as a part of ternary expression, there would be no conflict with existing code.

It was probably the choice of words (({each})) and (({do})) that bothered me. (({each})) sounds more like a name of a quantifier that should be followed by parameter names, than like a name of a method.

After all, in method definitions the list of parameters is not separated from the body either:

def foo(x) x*x end

proc do |x| x*x end
=end

#10 Updated by Alexey Muranov 11 months ago

=begin
I do not mind this being closed, i do not see how this can be made consistent with the existing syntax, and probably my reason for suggesting this was not valid, but as a side note, i want to mention that i have just discovered that in Combinatory Logic, when a lambda term from Lambda Calculus is converted to an equivalent combinatorial term, an alternative intermediate notation used sometimes for "(({λx.M}))" is "(({[x].M}))":
((URL:http://plato.stanford.edu/entries/logic-combinatory/#2.3)).
=end

#11 Updated by Zachary Scott 11 months ago

  • Status changed from Feedback to Closed

See

Also available in: Atom PDF