-
Notifications
You must be signed in to change notification settings - Fork 62
shortcut syntax for partial function application #7351
Comments
Would it screw up the language grammar a bunch if we made it so the value formatHex => Integer.format(value, 16);
strings.map(value.initial(3)); Or would that get rapidly ambiguous? |
@CPColin I'm not sure, I would have to try it. And then I would have to decide if I actually like it ;-) |
A quick attempt at this choked ANTLR, but I don't have time right now to dig into precisely why. |
Well that's a good sign!
Yeah, I could see giving an extra meaning to the |
@CPColin no, "choked" as in resulted in this error:
i.e. it looked ambiguous to ANTLR. |
Sorry, I should've italicized "that," to make my lame sarcasm more clear. |
I think it is a good idea to be extremely careful with syntactic sugar magic that makes the language less explicit and more compact, because it also makes the language less regular and more tricky to understand. As a counter-proposal I'd like to suggest this: Infer the lambda parameter type (like in Java), then the example above already is pretty short, and it follows the fundamental pattern of variable declarations better:
With the term "the fundamental pattern" I mean that you first declare a variable and give it a name, then you can use it. ( About this suggested syntax
I think this is really problematic! Because the useful syntactic distinction between a lambda expression and a function call expression is lost. These two things are so different so I think it is really useful for readability to have distinct syntax for them. In this example it is not any more so immediately clear to a reader if I think this syntax is pretty bad in this simple example, and it quickly become worse in a little more complex cases. For example:
This (I think) would be a function which returns a function. I also think it sounds weird that |
After seeing the version with soft keyword ‘it’, I tend to agree to a degree with @jensli here that the keyword ‘it’ does not really bring out the fact that we are dealing with a partial application here. If we stay with keyword approach, I would much prefer it if ‘value’ could be used - along with the keyword like treatment in the rest of the language it is much less likely that it would be confused with a local variable name than ‘it’. Other than that I do have a preference for using ‘_’ (underscore) as a partial application wildcard. I’ve always seen it as a meaningless sigil to replace some variable name whenever I mean to say “I don’t care what is the input; Just do this” |
To be clear: I don't plan to implement this feature unless it is extremely clear that it's very popular. The only reason this really came up is that apparently JavaScript is looking at adding something like this. I definitely don't plan to start work on it right now.
That is a good suggestion. In Ceylon (unlike in language like ML and Haskell) we don't typically infer the types of things from how they're used, since that doesn't generally interact very well with subtyping. However, we've just identified a restricted case in which anonymous function parameter type inference from usages does look pretty robust. If the parameter is only used as a function argument, and never:
then we could reasonably infer its type from how it's used. I like the idea, and I think we should open an issue for that. OTOH, it would only get us part of the way to "partial application". ((n) => Integer.format(n, 16)) is still significantly more verbose than: Integer.format(it, 16) But maybe it's good enough. |
If a short-cut syntax for lambdas is to be introduced, then I think it is best to make it syntactically different from normal function calls in some easily spotted way. Maybe this syntax can be used:
That is, the It looks a bit weird in an assignment, but better as a function argument:
But I myself think this is too implicit and weird and probably create more confusion that clarity. |
Now that we have #7058, I feel like the pressure for this feature is significantly alleviated. |
For the record, I dislike the keyword |
@jensli please see #7190 (comment) |
Currently, it's easy enough, but kinda verbose, to partially apply a function:
The discussion in #6615 raised the possibility of having a shortcut syntax for the special but very common case where we're partially applying a function of multiple parameters and obtaining a function with exactly one parameter.
Basic proposal
The proposed syntax would be, approximately:
Where
it
is a "magic identifier" or "soft" keyword. (That is, if there is nothing namedit
declared in the current scope, it would be given this special interpretation. Furthermore, a warning would be produced wherever the nameit
is declared.)A reasonable objection to this proposal is that it is much too much of a special case:
I think these objections are reasonably dispatched by noting that:
Possible extension
An open question is whether this feature could be extended to the receiving instance of a method invocation, in contexts where the receiving instance type can be inferred, for example:
would mean:
and:
would mean:
I'm not sure about that, but it's worth considering.
The text was updated successfully, but these errors were encountered: