question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

infer anonymous fun parameter type from usage

See original GitHub issue

As 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:open
  • Created 5 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
gavinkingcommented, Apr 15, 2018

I realized that we can also do some good stuff with some operator expressions, for example:

Float(Float) f = (x) => x/2.0;
Boolean(Float) g = (x) => x>2.0;
Boolean(Boolean,Boolean) h = (p,q) => !p || q;

I’ve implemented this sort of inference for the operators:

+ - * / % ! || && ** .. < > <= >= <=> == != === = += -= *= /= %= ||= &&=
2reactions
someth2saycommented, Apr 16, 2018

@gavinking

The question I have is whether stuff like the following should be accepted or rejected:

Why shouldn’t both be accepted? format(n,...) implies n should be Integer, and format(n+100... implies n should be Summable (or Numeric, not sure…) In any case, the union for both restrictions (Integer & Summable) should generate the same type (Integer), that is the right type for n, isn’t it? As long as union is commutative, the order of restrictions should not matter, IMHO.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found