-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Impove type inference for yields in computation expression to let them be aware of overloads with function parameters #1388
Comments
It isn't, likely L This is not only about yields in CE, this is more fundamental problem, we need to choose yield overload in this case, to do that we need to know the type of its argument, which we can't know because we can't typecheck it (we can't choose Let's say we make a change (special case the function somehow) to do type inference differently in certain cases, what happens when another overload is added? open System
type Test() =
member inline _.Yield(value: int -> unit) = ()
member inline _.Yield(value: bool -> unit) = ()
member inline _.Yield(value: string) = ()
member inline _.Zero() = ()
let x =
Test() {
yield (fun y -> Console.WriteLine(y))
} Which overload should be chosen in this case, and which type should be inferred for |
As soon as the "exactly one function overload" stops being true, it would go back to being a method resolution error needing additional type annotations. This is not unlike other method resolution situations where adding a new overload causes a compilation error downstream (looking at BCL adding overloads). |
My understanding (as soon as I don't know current implementation details) are the following: 1st case: it's even simpler to me, since we have a finite set of possible overloads in 2nd case is more complex to me, since there is no set of possible overloads, since the type is totally uknown, we only know that
Will change, no problem
As I said, my thoughts above are very naive since I don't know details of how typechecker implementation works today, so they easily might be impossible. |
This suggestion is based on issue dotnet/fsharp#17837
The following code is not currently allowed, it requires specifying
int
type fory
explicitly.another case:
I suggest that this is undesired behavior and typechecker should be able to try function overload before giving up and saying that type is unknown. The suggested changes in typechecker by @T-Gro:
-The callsite is known to be passing in a function (not yet fully resolved, but known to be a function)
-There is exactly one overload with a function type argument (although I'm not sure why this is needed, I hope that typechecker can go through several overloads with function arguments and still resolve if there is only one match without error)
Pros and Cons
The advantage of making this adjustment to F# is less verbose code and consistent auto-inference behaviour.
The disadvantage of making this adjustment to F# is more work for typechecker
Extra information
Estimated cost (XS, S, M, L, XL, XXL): L
Affidavit (please submit!)
Please tick these items by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: