blue-sky
blue-sky

Reputation: 53856

Compile error when adding List as parameter

This code :

package neuralnetwork

object hopfield {
  println("Welcome to the Scala worksheet")

  object Neuron {
    def apply() = new Neuron(0, 0, false, Nil, "")
    def apply(l : List[Neuron]) = new Neuron(0, 0, false, l, "")
  }

  case class Neuron(w: Double, tH: Double, var fired: Boolean, in: List[Neuron], id: String)

  val n2 = Neuron
  val n3 = Neuron
  val n4 = Neuron
  val l = List(n2,n3,n4)
  val n1 = Neuron(l)



}

causes compile error :

type mismatch; found : List[neuralnetwork.hopfield.Neuron.type] required: List[neuralnetwork.hopfield.Neuron]

at line : val n1 = Neuron(l)

Why should this occur ? What is incorrect about implementation that is preventing the List being added ?

Upvotes: 2

Views: 48

Answers (1)

Ende Neu
Ende Neu

Reputation: 15783

You are passing in the type, n2, n3 and n4 have type Neuron.type, try adding the parenthesis:

val n2 = Neuron()
val n3 = Neuron()
val n4 = Neuron()
val l = List(n2,n3,n4)
val n1 = Neuron(l)

The difference is that with the parenthesis you actually get a Neuron class (you call the apply method) and the type will be Neuron instead of Neuron.type.

Edit:

The .type notation is called singleton type, it denotes just the object represented by the class, in this case Neuron.type returns just that singleton object, more infos are in this paper by Odersky about the scala overview at page 9.

Upvotes: 4

Related Questions