2

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 ?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, otherwise call the apply manually as Gabriele said (I like parenthesis better).
I like the parenthesis better too, actually. It's worth saying that since apply takes no parameter, the compiler needs to choose between Neuron.apply and Neuron as a type when it sees Neuron, and it will always choose the latter, since otherwise you would have no way of referring to the type of Neuron.
@Ende Neu is .type a method ? I cannot seem to find its use in Scala API?
@blue-sky actually is not, but there's not much around, the best I could find is this post.
@blue-sky I found out something about the .type notation and made an edit to my answer, something more detailed can be found in that paper (you don't have to read through it if you're only interested in the singleton type notation).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.