Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"def someFun[_] (a:Int) = a", No warnings and no errors. normal?

Out of curiosity I tried to run the following:

def someFun[_](a:Int) = a

To my surprise, no errors or warnings got issued and it runs the way you expect it to (which is fine I suppose) but is it normal that the compiler does not understand the redundancy of the type parameter or perhaps it means something that makes it (semantically?) different from this:

def someFun(a:Int) = a
like image 642
Ashkan Kh. Nazary Avatar asked Jan 17 '23 11:01

Ashkan Kh. Nazary


1 Answers

The compiler generates this:

def someFun(a: Int): Int = a;

But this is not because the compiler knows that this is unused, but because of the type erasure. You can check things like this with the -print option of the compiler. It is also not surprising, that this works, because it is just an unused type parameter.

like image 158
drexin Avatar answered Jan 21 '23 23:01

drexin