0

I have a simple toy project made of two classes:

object A {
    var b = B()
}

class B {
....
}

I cannot use any IDE, because I need to use ssh.

I cannot compile the project because of the error:

A.scala:18: error: not found: value B

I compile using:

scalac *.scala 

I tried to play around with the classpath but that did not solve.

Can you help me?

0

2 Answers 2

4

Since you haven't defined a companion object for B, when you instantiate it, you need new B() instead of just B().

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

Comments

3

If you want to instantiate class B by using var b = B(), you'll need to create a companion object for class B with an apply() factory method:

class B {
  // ...
}

object B {
  def apply(): B = new B()
}

Note that B() is special syntax for B.apply().

Another way is to make B a case class:

case class B {
  // ...
}

Then a companion object with an apply method is automatically generated (as well as a toString and methods for pattern matching).

Comments

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.