Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8939281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:47:26+00:00 2026-06-15T10:47:26+00:00

In Scala in Depth there is this example: trait Logger { def log(category: String,

  • 0

In Scala in Depth there is this example:

trait Logger {
  def log(category: String, msg: String) : Unit = {
       println(msg)
  }
}

trait DataAccess {
  def query[A](in: String) : A = {
     ...
  }
}

trait LoggedDataAccess {
  val logger = new Logger
  val dao = new DataAccess

  def query[A](in: String) : A = {
     logger.log("QUERY", in)

     dao.query(in)
  }
}

I am a little confused by the initialization of Logger and DataAccess in the trait LoggedDataAccess. In the REPL, when I type out this code, I get the following exception:

 error: trait Logger is abstract; cannot be instantiated
       val logger = new Logger

Can a trait actually be initialized like that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:47:27+00:00Added an answer on June 15, 2026 at 10:47 am

    Trait can’t be instantiated, but you can create an instance of anonymous implementation of trait:

    scala> trait Test
    defined trait Test
    
    scala> new Test
    <console>:9: error: trait Test is abstract; cannot be instantiated
                  new Test
                  ^
    
    scala> new Test{}
    res0: Test = $anon$1@7fafd333
    
    scala> new Object with Test
    res1: Test = $anon$1@4fe11d82
    

    new Test{} and new Object with Test means the same. They create new anonymous class and instantiate it right away.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the book Scala in Depth . There's this example of implicit scoping as
There's an example in Scala in Depth where the author is explaining how scala
scala> class A (s: String*) { val l: ListBuffer[String] = ListBuffer[String](s) } <console>:8: error:
scala> class A defined class A scala> trait B defined trait B Creating an
Scala programmer should have known that this sort of writing : class Person{ var
In Scala, I can define structural types as follows: type Pressable = { def
Chapter 2 of the new Manning book, Scala in Depth by Josh Sueresh is
I have written a simple depth-first search in Scala with a recursive function like
From Josh Suereth's Scala in Depth: Applicative functors provide a way to take two
I tried running the following snippet from Scala in Depth in the REPL: val

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.