There is no any images today, except maybe one. I particularly have a little brainstorming and maybe somebody would be interested too. Let’s try to scarcely touch the Scala type inference on small practical example.
In the Scala code snippets below I will provide a three methods with a short explanation in terms of the Scala type inference.
The first one is pretty straightforward, we’re defining some trivial logic and eventually Scala compiler (and interpreter, of course) will infer our return method type as Int, thus, there is no need to declare it explicitly. Here we go:
//------------------------------------------------------------------------------------------- // 1. A simple one //------------------------------------------------------------------------------------------- def max2(x: Int, y: Int) = if (x > y) x else y
If we’ll define this method in the Scala interpreter and will make a call of the method by passing an Integer parameters, we’ll get something like this:
scala> def max2(x: Int, y: Int) = if (x > y) x else y max2: (x: Int, y: Int)Int scala> max2(23,145) res0: Int = 145
Pretty easy. Our return type is Int, as mentioned above. By going further, the second method is simply a side effect of the method call and return type of the method always would be a Unit (a void in Java):
//------------------------------------------------------------------------------------------- // 2. Side effect of the useless method call //------------------------------------------------------------------------------------------- def greet() = println("Hello, world!")
Declaring and running in the Scala interpreter:
scala> def greet() = println("Hello, world!") greet: ()Unit scala> greet() Hello, world!
Hence, this method is pretty useless for us, but it shows how Scala compiler infers return types.
And the last one is an interesting thing… A type annotation must follow every function parameter, preceded by a colon, because the Scala compiler (and interpreter) does not infer function parameter types. A one interesting example initially was mentioned by Andrey Breslav (and maybe by somebody else, I don’t know), that makes a surprising revelation about Scala type inference. To not digress from a context and to be abridged, here is the example that deserve to think about:
//------------------------------------------------------------------------------------------- // 3. Interesting //------------------------------------------------------------------------------------------- class InferTest { def foo_bar[T](x: T): T = x val b = foo_bar() }
Just stopping by and try to think at this point. As already mentioned, Scala does not infer a function parameter types. Therefore, we’re trying to make a call without a parameter declaration and the question is: is this code will compile and if yes, then what is the type of ‘b’ ? Is it will be a Unit type, since we’re making a call of the method without a parameter (probably a Unit type) ? What do you think?
Your mind would be hamble fast as you’ll see a result. I’ll try to make it step by step. Firstly, let’s define a function in the Scala interpreter and make a method call with a String type:
scala> def foo_bar[T](x: T): T = x foo_bar: [T](x: T)T scala> foo_bar("Wow cool!") res2: java.lang.String = Wow cool!
Secondly, let’s make the same, but by passing Integer type instead:
scala> foo_bar(9955) res3: Int = 9955
Surprisingly very simple. A tension on both cortex hemispheres will grow up dramatically, when we’ll try to make a compulsive method call without a passing through any parameters. Ready?:
scala> foo_bar() scala>
I was very astonished by seeing this result! It is even not a Unit type, it’s a type of nothing, a black hole. But, we can make a call.
Conclusion
As a corollary, you might see, that a confine of type safeness is slightly blurred for now and could even cause a sleep deprivation in some cases. Scala type safeness and complexity are still becoming to be a point of contention ever since. However, it does not mean, that you should expunge this wonderful language from your toolbox, instead you must to be aware of. I am wholeheartedly wish you not be trapped in such situations, but who knows…
Don’t know how are you, but me slightly suffering from a finger and mental fatigue at the end of this post. So, that’s enough for today!