0

When trying to extend an class with a trait with concrete definition like so

trait logmsg
{
  printmsg(msg : String)  = println(msg)
}

class DimensionUtils extends logmsg {
  printmsg("hello")
}

why does the repl give the following error:

**error**
defined trait logmsg
<console>:16: error: not found: value printmsg
          printmsg("hello")
2
  • 5
    this code is correct. I'm guessing you are pasting the code into the REPL, and the problem will go away if you move the first { onto the same line as trait logmsg, to prevent the REPL from taking trait logmsg by itself as a complete definition. this is one of the reasons Scala programmers never put { on a line by itself. (the other is semicolon inference.) Commented Oct 3, 2018 at 20:19
  • 1
    Moreover looking for scala style guide You should have trait starting with capital letter. In 90% situation put the parenthesis in the same line, not a new line. Remove all necessary lines. Your code can have 7 lines with the same information. Commented Oct 5, 2018 at 12:09

1 Answer 1

1

Your Code is correct, I tried in eclipse editor by creating a Scala project, getting an answer as "hello" here is the code which runs.

object StackQ extends App {
  val obj = new DimensionUtils
}

trait log_msg {
  def printmsg(msg: String) = println(msg)
}

class DimensionUtils extends log_msg {
  printmsg("hello")
}
Sign up to request clarification or add additional context in comments.

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.