infer anonymous fun parameter type from usage
See original GitHub issueAs suggested by @jensli in #7351, we could enhance anonymous function parameter type inference to take usages into account. For example, in:
(n) => Integer.format(n, 16)
The parameter n
would be inferred to have type Integer
.
Quoting myself:
If the parameter is only used as a function argument, and never:
- as the receiver of a method invocation, nor
- in an operator expression, nor
- as the argument of an overloaded (Java) method,
then we could reasonably infer its type from how it’s used.
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (11 by maintainers)
Top Results From Across the Web
Go 2: spec: shorter, type-inferred anonymous function syntax ...
Who does this proposal help, and why? Anyone trying to call functions that take a simple function as an argument. What is the...
Read more >Why type inference is not able to infer a type in a function ...
Since you don't supply an explicit type parameter when constructing Foo , Foo 's generic type argument T must be inferred from its...
Read more >Typescript: Type Inference on function arguments
In this article I will show you simple and very useful examples of type inference in TypeScript. Imagine you want to infer not...
Read more >Documentation - More on Functions - TypeScript
Use Fewer Type Parameters ... We've created a type parameter Func that doesn't relate two values. That's always a red flag, because it...
Read more >Type inference - Kotlin language specification
Kotlin has a concept of type inference for compile-time type information, meaning some type information in the code may be omitted, to be...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I realized that we can also do some good stuff with some operator expressions, for example:
I’ve implemented this sort of inference for the operators:
@gavinking
Why shouldn’t both be accepted?
format(n,...)
impliesn
should beInteger
, andformat(n+100...
impliesn
should beSummable
(orNumeric
, not sure…) In any case, theunion
for both restrictions (Integer & Summable
) should generate the same type (Integer
), that is the right type forn
, isn’t it? As long asunion
is commutative, the order of restrictions should not matter, IMHO.