A C#-style syntax for function signatures #13220
Labels
Area-Language Design
Language-C#
Resolution-Duplicate
The described behavior is tracked in another issue
After reading the Ceylon language tour http://ceylon-lang.org/documentation/1.2/tour/functions/ I realised that they have a very nice way of representing function signatures (i.e. the type of a lambda function) using a type-first, C-style syntax.
Currently, we have to use the Func<> and Action<> types if we wish to write a higher order function, such as in this fictional MyList class:
but using this new syntax we can write
Func<TArg, TReturn> function
asTReturn function(TArg)
, which is easier to understand as it looks a lot like a method signature on a class. Also, we can represent actions usingvoid function(TArg)
. If we apply this to the example above we get:In addition to the advantages already mentioned this would allow us to name the arguments in our functions. Sometimes the type alone isn't helpful, such as the Select overload that takes a
Func<int, TElement, TResult>
. If we are dealing with anIEnumerable<int>
then the signature is Func<int, int, TResult> and we can easily get confused as to which lambda argument is the index and which is the element from the list.If we can declare pseudonames for the lambda arguments (using the same magic as the new named Tuples?) then, lifting from Ceylon again, we would be able to write:
And the calling code would be able to distinguish between the lambda arguments because instead of seeing the signature as
Func<int, int, TResult>
they would seeTResult selector(int index, int element)
.You could also used named & positional parameters when calling one of these lambdas. Currently, when calling a lambda with the signature
Action<int, int>
, you see the signature.Invoke<int, int>(int arg1, int arg2)
, for example. Under the new system I would be able to write:The text was updated successfully, but these errors were encountered: