Reputation:
When is it advisable to let the compiler do its thing and when should I be explicit when declaring variable types?
Upvotes: 4
Views: 164
Reputation: 118865
I agree with @Stephen, let the compiler "do its thing".
When you are first starting with a type-inferred language, this will feel unnatural and you'll write extra type annotations, perhaps thinking you need them for readability. You'll get over that soon enough; code with decent variable names has little need for spelling out types everywhere, type annotations are often just excess cruft cluttering your algorithms.
I can only think of a couple general detriments to not spelling out the types. First, if you are viewing the source code as a mere text file, it may not be obvious to the reader what the types are. However this is mitigated very largely by the fact that tools like Visual Studio provide hover-tooltips that show the types (e.g. hover your mouse over foo
and a tooltip pops up showing the type of foo
). The logic for doing this inference is exposed by the compiler source code, and has been easily integrated into other tools, like F# web snippets, MonoDevelop, etc. (Someone, please leverage the new Apache license and write the plugins for github, emacs, and gvim :), thanks!) As a result, much of time you're looking at the code, you'll be doing it in an environment/tool where the types are available to be shown on-demand anyway.
Second, occasionally it can be hard to debug type errors when there is a lack of type annotations. When you get a weird type inference error you can't figure out, it can be useful to add some explicit annotations to localize the type error. Oftentimes you'll then see some dumb bug in your code, fix it, and then you can remove the needless annotations.
Of course, there are a number of places where type annotations are required because type inference can't solve everything. See the links below for some details there. You'll get accustomed to these after a while, and get adept at predicting when you will or won't need an annotation.
http://lorgonblog.wordpress.com/2009/10/25/overview-of-type-inference-in-f/
Why can't F#'s type inference handle this?
Upvotes: 4
Reputation: 22297
Easy, in F#, always prefer to let the compiler "do its thing". The folks who wrote its powerful type inference system would be saddened otherwise.
In all seriousness, in C# I know there is (or was?) a debate about when or when not to use var
for infered variable types. But I believe the concerns there about the lack of clarity stemmed from a community which was unfamiliar with terse, strongly typed languages and feared var
was some kind of dynamic voodoo not to be trusted. But what we have now in C#, and many times over in F#, is the best of all worlds. Strong, automatic typing. And "variables" are only the tip of the ice-burg. The real amazement comes with F#'s inference of function type signatures. There was a while there where I believed this was over-the-top, and that writing out the full signature would be clearer. Man, you get over that fast.
Upvotes: 8