Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I would like to know your opinion on declaration by hand of parameters/return types in languages with type inference like Scala. Is there any reason why to or not to do this when compiler can infer types?

share|improve this question
2  
Readability. Just because the compiler can infer the type, doesn't mean a human reader of your code can. Sometimes it's nice to see explicitly what type one is dealing with. –  PersonalNexus Jan 9 '12 at 16:54
1  
As above, but with a different twist: machine readability. For example to make it easier to document a library with a tool. –  Yttrill Jan 13 '12 at 19:54

1 Answer 1

I cannot speak for Scala (but I think what I write will apply there, too), but for example Haskell's type system has the property that it infers the most general type, yet this is not always what you want. Consider this simple example:

sqr n = n * n

The type checker infers this type for sqr

Num a => a -> a

which means that if you pass a value of numeric type (Num a) then you'll get a result of that same type. However, sometimes you want a more specific type. For example, you don't want the compiler to generate code that can square Double, Complex, Integer or a handful of other types, as this incurs some overhead. Thus, when you need sqr a zillion times in your code, you might want to state:

sqr :: Double -> Double

to get a more specific version.

Another point are higher rank types, type inference for higher rank types is undecidable for ranks > 1, hence if you have a higher rank function, you actually need to annotate it. The compiler can then check the type, but it can't infer it. The type system of Frege is similar to that of Haskell 2010 with the addition of higher ranked types, hence the same applies here.

share|improve this answer
2  
Re your first point: In theory, compilers can generate specialize versions in addition to the general case (of course only for instances it knows about when compiling the function, and if done carelessly it results in code bloat), giving all the performance benefits without loss of generality. I believe GHC does so in some - though obviously not all - cases, and there's a pragma to enforce it (SPECIALIZE). I haven't written enough number crunching benchmarks to tell whether this makes manual specializing obsolete. –  delnan Jan 9 '12 at 17:44
    
@delnan The Haskell 2010 report contains also an example where the most general type is not correct (or at least fairly absurd), independend of type classes. The point I am trying to make is that type annotations can be useful, even indispensable, apart from merely better readability. –  Ingo Jan 9 '12 at 18:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.