Adventures in F#--Probing Type Inference
Jomo Fisher--I was curious about type inference in F# and I wondered what would happen if there was really no way for the compiler to infer a type. Consider this function which takes a value and just returns it:
let func n = n
Like last time, I compiled this with fsc.exe. I was expecting a polite error message saying the type of 'n' couldn't be determined. Instead, compilation succeeded. The result was equivalent to this C#:
public static T func <T>(T n)
{
return n;
}
In retrospect, this makes complete sense. Of course an unknown type should be inferred to be a template type. So far, F# seems to do a good job of being concise for a strongly typed language.
This posting is provided "AS IS" with no warranties, and confers no rights.
Comments
Anonymous
September 13, 2007
Here's my vote for a wildcard type specifier in C#!Anonymous
September 13, 2007
So on the off chance he (the guy I met, not the cat's father) is reading my blog, I thought I'd point him in the direction of Jomo Fisher's new post s on F#....Anonymous
September 15, 2007
Try: let f (a, b) = (b, a)Anonymous
September 16, 2007
Wondering, have you tried debugging for the actual type being infered? https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=291513Anonymous
September 16, 2007
Nelak, I haven't tried VS integration at all at this point. I'm using notepad.exe and fsc.exe. I do plan using VS integration once I understand the language well enough. The reason is that I want to think about and then predict what I would like in an IDE experience for F# first.Anonymous
September 17, 2007
I was not referring to debugging with VS, but to check that calling the method with type inference, like: int n; Func(n); would return the type you actually expected to, which should be an int. Actually my tests on Framework 2.0 show the type being infered is Object, and has been acknowledge as an existing bug in orcas beta2 also. My guess is the issue probably spreads to other implementations, so actually you are not getting real type inference, even though I agree the code compiles, but you won't get the expected behaviour while running it.Anonymous
September 17, 2007
The comment has been removedAnonymous
September 17, 2007
Nelak, Though it may be counterintuitive depending on your background with other languages, the second set of code you posted is supposed to be a compile error. The static type passed to func is System.Object and the return variable is System.Int32. By the definition of the function they must be the same. Template parameter resolution happens at compile time in C# not runtime. All the compiler knows at compile time is that 'obj' is type System.Object.Anonymous
September 17, 2007
The comment has been removedAnonymous
September 17, 2007
Hi Nelak, Unfortunately, I can't access the attachment on that connect bug and the rest of the bug doesn't stand on its own without it. I did run your code above and it prints 0 as expected for me. Do you see a different result?Anonymous
September 17, 2007
Jomo Fisher-- Easily my favorite feature of F# so far is the combination of discriminated union and patternAnonymous
September 25, 2007
The comment has been removed