9 Responses to “Thoughts on Scala Type Inference”

  1. Kipton Barros 07. Aug, 2011 at 9:44 pm #

    The Scala interpreter with the “:type” command can help resolve this puzzle. Here’s what I get:

    ————————————–
    scala> def f[T](x: T): T = x
    f: [T](x: T)T

    scala> :type f()
    Unit

    scala> :type ()
    Unit
    ———————————

    It turns out that () is the single instance of type Unit, and when you pass it to f, it’s also what you get back. The Scala interpreter doesn’t print out (), leading to the confusion.

  2. Alexander Pupeikis 08. Aug, 2011 at 3:19 am #

    It’s not only a confusion, it’s weird behavior from the design perspective. We may not only make a method call without passing any parameters to our previously declared method, but moreover Scala interpreter doesn’t provide any feedback and not throwing an exception in such case. Frankly, slightly misleading. Maybe this is a “feedback” from the Scala feature – each method is object and each operation is actually a method.

    Thank you for your note and explanation! Good to know this.

  3. Steve 10. Aug, 2011 at 11:58 am #

    “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.”

    Really? Which version do you use? In every Scala version I tried, foo_bar() is Unit and not Nothing, as easily seen by assigning it to a value:

    val foo = foo_bar()
    foo: Unit = ()

  4. Alexander Pupeikis 11. Aug, 2011 at 3:42 am #

    @Steve you absolutely right. When we’re assigning a result from this method call to a value, it prints out a type of ‘()’ operation (a Unit type, as expressed by Kipton Barros above), but I guess not a type of our method. It’s still weird, that we can make a call of the method without passing through any parameters. I thought about this and I guess, that Scala interpreter in such case don’t actually calling our method, but instead assigning a type of the ‘()’ operation. Maybe, it would be right to throw an exception instead and don’t provide a confusion? => http://min.us/ll4KVS

  5. Christopher Taylor 12. Aug, 2011 at 2:32 pm #

    I think what’s happening is that the scala interpreter is able to parse the ‘no param’ call because scala doesn’t require you to use parens on method calls. For example, instead of

    foo_bar(“Wow cool!”)

    you could have written

    foo_bar “Wow cool!”

    instead. So when you write

    foo_bar()

    the interpreter parses this as if you had written

    foo_bar ()

    which is equivalent to

    foo_bar(())

    where — as Kipton and Steve explained — () is the sole instance of type Unit, which in turn determines the return type.

  6. Alexander Pupeikis 14. Aug, 2011 at 6:43 pm #

    @Christopher And this only adds to the confusion, more and more and more. This should be more clear, simple and non opaque. Some things in Scala much easier than ever, but some of them are very messy.
    Nothing is ideal and we shouldn’t fight for the idealism. So, can live with that. Maybe in the next releases and after new investigations, such things would be more transparent. I love Scala, whatever.

    PS: sorry, that I didn’t approve your comment as fast as possible. He just falls down into a spam somehow… Both of them…

  7. Alexander Pupeikis 22. Sep, 2011 at 1:27 am #

    Just occasionally found a simple explanation on this terms.

    The Nothing type has no instances. It is occasionally useful for generic constructs. For example, the empty list Nil has type List[Nothing], which is a subtype of List[T] for any T.

    The Nothing type is not at all the same as void in Java or C++. In Scala, void is represented by the Unit type, the type with the sole value ().
    Also, Unit is not a supertype of any other type. However, the compiler still allows any value to be replaced by a (). Consider:

    def printAny(x: Any) { println(x) }
    def printUnit(x: Unit) { println(x) }
    printAny("Hello") // Prints Hello
    printUnit("Hello")
    // Replaces "Hello" with () and calls printUnit(()), which prints ()

  8. property man 17. Jan, 2012 at 12:10 am #

    With havin so much content do you ever run into any issues of plagorism or copyright infringement? My site has a lot of unique content I’ve either written myself or outsourced but it seems a lot of it is popping it up all over the web without my authorization. Do you know any ways to help stop content from being ripped off? I’d definitely appreciate it.

  9. Alexander Pupeikis 26. Jan, 2012 at 11:04 am #

    @property_man I don’t care about license. It should to be copyleft in any fashion. Humans’ knowledge and evolution are more important, than your own self-gratification. If you want to protect something – just keep it in private and don’t tell anybody.