Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there disadvantages to return type inference? If yes, what are they?

A lot of statically typed languages, like C++ and C#, have local variable type inference (with the keywords auto and var respectively, I think).

However, I haven't seen many C-derived languages (apart from those mentioned in the comments) implementing compile-time return type inference. I'll describe what I mean by "return type inference" before I ask the question. (I definitely don't mean overloading by return type.)

Consider this code in a hypothetical C#-like language:

private auto SomeMethod(int x)
{
    return 3 * x;
}

It's more than obvious (to humans and to the compiler) that the return type is int (and the compilers can verify it).

The same goes for multiple paths:

private auto SomeOtherMethod(int x)
{
    if(x == 0) return 1;
    else return 3 * x;
}

It's still not ambiguous at all, because there is already an algorithm in said languages to resolve whether two expressions have compatible types:

private auto YetAnotherMethod(int x)
{
    var r = (x == 0) ? 1 : 3 * x;
    return r;
}

Since the algorithm exists and it is already implemented in some form, it's probably not a technical problem in this regard. But still, I haven't seen it anywhere in statically typed languages, which got me thinking about whether there's something bad about it.


My question:

  • Does return type inference, as a concept, have any disadvantage or subtle pitfall that I'm not seeing? (Apart from readability - I already understand that.)
  • Is there some corner case where it would introduce problems or ambiguity to a statically typed language? (By "introduce", I'm referring to issues that local variable type inference doesn't already have.)
like image 992
Theodoros Chatzigiannakis Avatar asked Mar 07 '14 13:03

Theodoros Chatzigiannakis


People also ask

Is type inference good or bad?

While type inference is really useful for local variables, it should not be used for public APIs which have to be unambiguously documented. And sometimes the types really are critical for understanding what's going on in the code. In such cases, it would be foolish to rely on type inference alone.

What is a type of inference?

Type inference refers to the automatic detection of the type of an expression in a formal language. These include programming languages and mathematical type systems, but also natural languages in some branches of computer science and linguistics.

What is the difference between type checking and type inference?

A Type Checker only verifies that the given declarations are consistent with their use. Examples: type checkers for Pascal, C. A Type Inference system generates consistent type declarations from information implicit in the program.

What is type inference in Haskell?

Type inference is the process by which Haskell 'guesses' the types for variables and functions, without you needing to specify these types explicitly. Many functional languages feature type inference. There is lots of theory behind type inference — Hindley-Milner type systems and Unification.


1 Answers

yes, there are disadvantages. one you already mentioned: readability. second - the type has to be calculated so it takes time (in turing-complete type systems it may be infinite). but there is also something different - theory of type systems is much more complicated.

let's write a function that takes a list and return its head. what's its type? or function that takes a function, and a parameter applies that and return the result. in many languages you can't declare it. to support this kind of stuff, java introduced generics and it failed miserably. currently it's one of the most hated features of the language because of consistency problems

another thing: returned type may depend on not only the body of the function but also context of the invocation. let's look at haskell (that has best type system i've ever seen) http://learnyouahaskell.com/types-and-typeclasses there is a function called read that takes a string, parse it and return... whatever you need, an int, an array.

so each time a type system is designed, the designer has to choose at which level she wants to stop. dynamic languages decided not to infer types at all, scala decided to do some local inference but not, for example, for overloaded or recursive functions and c++ decided not to infer the result

like image 114
piotrek Avatar answered Nov 15 '22 07:11

piotrek