Chuck wrote:Currently it does not. The syntax I'm contemplating is:
.foo(x, do(a is int, b as int), do(s as String))
do
return a + b
do
return s.trim.toLower
So if you have more than one anonymous method then you have a "do" for each one and put the block of code underneath that.
Feedback is welcome.
I like this idea very much. I think Guido Van Rossum once said that multiline lambdas were impossible syntactically, but maybe this option did not occur to him.
Since lambdas are used for the conciseness they bring, I would vote for the following shortcut version that would be used when only one delegate is expected:
.foo(x, do(a,b)) # parameter types implicit, would not work in case of ambiguity (i..e, more than one foo that takes an "x" and a two param. delegate)
result = a + b
return result
Note that this reminiscent of Ruby's block parameters, or whatever they're called. So much, that we could add an elegant "each" extension method to IList<of T>:
names.each(do(name))
print name
I know this can be achieved with "for name in names...", but this feature would allow for a much greater flexibility. For example, a count method for collections:
n = names.count(do(name)) # n = strings in "names" with length > 10
return name.length > 10
Finally, maybe Ruby's || syntax could lead to cleaner code. Judge for yourselves:
names.each(|name|)
print name
n = names.count(|name|)
return name.length > 10
We could even drop the parentheses when all they do is wrap a pair of "|"s.
Chuck wrote:Also, how often are you encountering this with .NET libraries (whether from MS or third party)?
Not very often, but I think that is because typical .NET languages are not too functional programming-friendly, and thus hinder the adoption of this kind of constructs. For example, an "each" method like the one above would be writable in C#, but its use would be to cumbersome to make it a viable alternative to a regular foreach loop. One feature I would definitely like to see in Cobra is a function that takes a delegate can be declared without having to declare the delegate first. For example, the count method above:
extend IList<of T>
def count(pred as |String -> bool|) as int
# implement the counting logic, calling "pred" for each item in the list
Note that I've made up a syntax for declaring a delegate on the fly.