List.type doesn't take parameters

Hello, I’ve just started to play around with Scala studying the red book (Functional Programming in Scala, Paul Chiusano, Rúnar Bjarnason).
There is a List example:
I’m sure I’m missing something very basic here.
On ListTest.scala I get the error List.type doesn't take parameters.

List.scala

      sealed trait List[+A]
      case object Nil extends List[Nothing]
      case class Cons[A](head: A, tail: List[A]) extends List[A]
      object List {
        def sum(ints: List[Int]): Int = ints match {
          case Nil => 0
          case Cons(x,xs) => x + sum(xs)
        }
      }

ListTest.scala

    object ListTest extends App {
        val x = List(1,2,3,4,5) match {
          case Cons(x, Cons(2, Cons(4,_))) => x
          case Nil => 42
          case Cons(x, Cons(y, Cons(3, Cons(4,_)))) => x+y
          case Cons(h,t) => h + sum(t)
          case _ => 101
        }

    }

List.type is the type of the List object. The error is saying
that that object isn’t something that you can call and pass parameters to.

“Things you can call” are (roughly) either methods or objects with
apply methods. In your case, if the List object had an apply method
that took a variable number of As and returned a List[A],
ListTest.scala would compile.

1 Like

thanks, indeed I was missing apply() method.
In the book is

   def apply[A](as :A*): List[A] =
       if(as.isEmpty) Nil
       else Cons(as.head, apply(as.tail: _*))

But why without that method I’m able to compile a ListTest like this?
println(List(1,2,3))

PS: unrelated: I noticed an exausting bug on Eclipse, often I see random error (red underlines) when there is no an actual error, indeed if I close and reopen the source file those errors disappears. There is a way to solve this annoying behaviour?

If Foo is an object, and it has a method named apply, then Foo(args) is treated as if it were Foo.apply(args).

1 Like

It’s hard to say without seeing all of the code, but note that there is
a class in the standard library called List with an apply method on
its companion object. That object might be what’s getting invoked.

1 Like

@ClientGilbert I posted the complete code.
I’m not sure it’s clear, I’m referring to the code of this book https://www.amazon.it/Functional-Programming-Scala-Paul-Chiusano/dp/1617290653 (~pag. 35, Data structures Chapter)

Another doubt: what exactly that Cons means? I know it stands for constructor, but I can’t see where I define this thing, I’m a little confused about that.
Also, in List.scala using sealed trait I define an interface, so why I need those pattern matching cases object Nil or class Cons[A]?

Just a general note: the Red Book is pretty advanced, and mostly assumes that you already know the Scala language. It’s not where I usually recommend folks begin to learn Scala. It’s more focused on teaching Scala programmers how to do functional programming.

Actually, it has nothing to do with constructor – that’s just an unfortunate accident of the names. “Cons” is a bit of ancient computer jargon (dating back to the 1960s), which basically means “an element of a List”. You define it in your code up top, as a case class. You’re essentially saying that a List is an abstract concept, which has two types under it: Nil, which is an empty List, and Cons, which is one list element that has more list after it.

1 Like

I would call a cons a list node or component. Usually, the elements of a collection are its values.