211
Ideas / Re: Macros again
« on: October 22, 2018, 09:22:08 PM »
If you look at Ruby, that uses blocks extensively, you need to be rather ad hoc in your implementation if you want to make it useful with closures.
Consider the following Ruby code:
In this case x([2, 2, 2]) would return 2 and y([2, 2, 2]) returns -1.
So even though the "times" method is defined using a lambda, there is special handling (return exits the outer scope) if the block is defined inline.
The closure/first class function version in y cannot express the same semantics. And usually "escaping the outer scope" is exactly what's so useful in many cases! This is also separate from the added complexity of optimizing away the lambdas, which is necessary for good performance.
Consider the following Ruby code:
Code: [Select]
def x(foo)
3.times do |x|
return x if foo[x] == x
end
return -1
end
def y(foo)
l = ->(x) { return x if foo[x] == x }
3.times(&l)
return -1
end
In this case x([2, 2, 2]) would return 2 and y([2, 2, 2]) returns -1.
So even though the "times" method is defined using a lambda, there is special handling (return exits the outer scope) if the block is defined inline.
The closure/first class function version in y cannot express the same semantics. And usually "escaping the outer scope" is exactly what's so useful in many cases! This is also separate from the added complexity of optimizing away the lambdas, which is necessary for good performance.